Recovery the state of the first activity launched


Last Updated:

  1. puxdroid

    puxdroid New Member This Topic's Starter

    Joined:
    Jan 15, 2010
    Messages:
    2
    Likes Received:
    0
    Hello.

    I have an application that launch a thread when a ToggleButton is pressed, but if I press the home button, a new activity is started (I can return to the first activity if I press the back button), but I want to recovery the first activity launched state, which have the reference of the initial thread.

    This is my code:

    org.test.test.java:

    ////////////////////////////////////////
    package org.test;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.ToggleButton;

    public class test extends Activity {

    private Thread mRefresh;
    private int mCounter = 0;

    private ToggleButton mStartStopButton;
    private TextView mTexStat;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mStartStopButton = (ToggleButton) findViewById(R.id.startstop);
    mTexStat = (TextView) findViewById(R.id.textStat);

    mStartStopButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    if (mStartStopButton.isChecked()) {
    mRefresh = new Thread(new Runnable() {
    @Override
    public void run() {

    while (true) {

    try {
    Thread.sleep(1000);// 1 second
    mCounter++;
    } catch (InterruptedException e) {
    Log.e("test", "Error sleeping", e);
    }
    }
    }
    });
    //Start the thread
    startThread();
    } else {
    // Stop thread an show the counter
    stopThread();

    }
    }
    });
    }

    private void stopThread() {
    mRefresh.stop();
    mTexStat.setText("Stopped, Counter = " + mCounter);
    }

    private void startThread() {
    mRefresh.start();
    mTexStat.setText("Started");

    }
    }

    ////////////////////////////////////////

    this is the mail.xml:

    ////////////////////////////////////////<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:eek:rientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:eek:rientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="3dip">

    <TextView android:layout_width="wrap_content"
    android:id="@+id/textStatus"
    android:layout_height="wrap_content"
    android:text="Status:" />

    <TextView android:text=""
    android:id="@+id/textStat"
    android:padding="3dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

    <RelativeLayout android:eek:rientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ToggleButton
    android:id="@+id/startstop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    />
    </RelativeLayout>
    </LinearLayout>
    </LinearLayout>

    ////////////////////////////////////////



    And this is my manifest.xml:

    ////////////////////////////////////////<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.test"
    android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".test"
    android:label="@string/app_name" android:clearTaskOnLaunch="false" android:allowTaskReparenting="false">
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    </activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

    </manifest>

    ////////////////////////////////////////



    Anybody knows how to solve this problem?

    Sorry for my English.
     

    Advertisement
  2. KeithG

    KeithG Well-Known Member

    Joined:
    Nov 15, 2009
    Messages:
    103
    Likes Received:
    3
  3. puxdroid

    puxdroid New Member This Topic's Starter

    Joined:
    Jan 15, 2010
    Messages:
    2
    Likes Received:
    0
    I have read this, but the system never execute onSaveInstanceState(), onli execute onResume() and onPause()
     
  4. KeithG

    KeithG Well-Known Member

    Joined:
    Nov 15, 2009
    Messages:
    103
    Likes Received:
    3
    Try implementing all 4.
     
  5. userands

    userands New Member

    Joined:
    Mar 12, 2012
    Messages:
    2
    Likes Received:
    0
    Scenario is as follows:
    I have an application with Activity A calling Activity B.
    When the user is in Acitivity B, and Home button is Clicked, the activity goes to the Background. The user then starts another heavy app like Map and operates that app for 10 minutes. The user then again comes to my Application and gets the Activity B screen - which works fine. However, when the user clicks the Button Back - Activity A shows a Black screen - No data is displayed.

    This issue doesnt occur when the User switches between activities for a small amount of time.

    Queries :
    (1) In Android : The Previous Activites are picked from the Back Stack. So is it that when the Acitivty is in Background for a long amount of time, Android OS clears it from the Stack. - If so How to detect that an Activity is killed from the Stack.

    (2) If an Activity is killed from Back Stack - How to recreate i.e How to create the List View again.
     
  6. userands

    userands New Member

    Joined:
    Mar 12, 2012
    Messages:
    2
    Likes Received:
    0
    Scenario is as follows:
    I have an application with Activity A calling Activity B.
    When the user is in Acitivity B, and Home button is Clicked, the activity goes to the Background. The user then starts another heavy app like Map and operates that app for 10 minutes. The user then again comes to my Application and gets the Activity B screen - which works fine. However, when the user clicks the Button Back - Activity A shows a Black screen - No data is displayed.

    This issue doesnt occur when the User switches between activities for a small amount of time.

    Queries :
    (1) In Android : The Previous Activites are picked from the Back Stack. So is it that when the Acitivty is in Background for a long amount of time, Android OS clears it from the Stack. - If so How to detect that an Activity is killed from the Stack.

    (2) If an Activity is killed from Back Stack - How to recreate i.e How to create the List View again.

    This issue is occuring only on Motorola Fire - Android phone
     

Share This Page

Loading...