Android Start activity without creating new instance

12,763

Solution 1

According to android activity life cycle, when you launch an activity A :

Life Cycle method list :

Activity A.onResume();
Activity A.onStart();
Activity A.onCreate();

Activity Status :

Activity A : Resumed

When you launch the activity B now :

Life Cycle method list :

Activity A.onStop();  
Activity B.onResume();
Activity B.onStart();
Activity B.onCreate();
Activity A.onPause();
    ...
    ...
    ...

Activity Status :

   Activity A : Stopped
   Activity B : Resumed

And when you again start A now :

Life Cycle method list :

Activity B.onDestroy();  
Activity B.onStop(); 
Activity A.onResume();
  ....
  ....
  ....      

Activity Status :

   Activity B : Destroyed
   Activity A : Resumed

This is the life cycle of an activity : enter image description here

You can find the details here

According to default behavior, activity A goes to onStop() state and doesn't alive and need to create a new instance on coming back to activity A. All I know - there is no way to keep alive of the A instance.

Solution 2

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

Try it... It makes old activity to front without new instance...

Solution 3

You Dont need do anything. Just remove your onBackPressed() method from Activity B.

By default when you move from Activity A to Activity B, Android adds the Activity A to the backstack. When you press the back button from Activity B or finish it, it automatically restore the Activity A from backstack.

If you wish to finish your Activity B programmatically call activity's finish() method when you want to do it.

Share:
12,763
Ibrahem Ahmed
Author by

Ibrahem Ahmed

Updated on June 27, 2022

Comments

  • Ibrahem Ahmed
    Ibrahem Ahmed almost 2 years

    Assume i have activity A that act as the root activity for my app . and form this activity i go to activity B.

    I want to be able to go back from B to A Without creating new instance of Activity A.

    this code is in Activity B

    public void onBackPressed() {
            super.onBackPressed();
    //      Intent intent= new Intent(getBaseContext(), MainActivity.class);
    //      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
            myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(myIntent);
            Log.d("Back", "TEST");
        }
    

    but it sill call the onCreate on activity A . What i want to do is have A in the background when activity b is started and whenever it is finished switch back to activity A

    this is the manifest

    <activity
                android:name=".MainActivity"
                android:label="@string/title_activity_main"
                android:screenOrientation="unspecified" 
                android:launchMode="singleTask"
                android:stateNotNeeded="false">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                     <category android:name="android.intent.category.HOME"/>
                    <category android:name="android.intent.category.DEFAULT" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity
                android:label="@string/app_name"
                android:name=".SubmenuActivty" >
            </activity>
    
  • Ibrahem Ahmed
    Ibrahem Ahmed about 11 years
    it will restore Activity a from the backstack by creating new instance of the it. i want to have Activity A alive all the time
  • Ibrahem Ahmed
    Ibrahem Ahmed about 11 years
    this doesn't keep activity alive it will just create new instance of the activity .. it will call the oncreate method again
  • Sankar V
    Sankar V about 11 years
    do u finish Activity A while starting Activity B? If not surely Activity A will get restored to its last state from backstack
  • Ibrahem Ahmed
    Ibrahem Ahmed about 11 years
    i dont i use Intent myIntent = new Intent(getBaseContext(), SubmenuActivty.class);startActivity(myIntent); to start activity A
  • Sankar V
    Sankar V about 11 years
    did u tried removing your onBackPressed() method from Activity B?
  • David Wasser
    David Wasser almost 11 years
    When A starts B, A is just stopped (but still alive). As soon as B finishes it will automatically restart and resume A. A will be alive until it calls finish() on itself.
  • David Wasser
    David Wasser almost 11 years
    What do you mean by "want to have Activity A alive all the time"? It is alive, it is just hidden by Activity B. Please define "alive". What is it that you want Activity A to do when Activity B is completely covering it and getting all user input?