Home » Android » java – Android Start activity without creating new instance

java – Android Start activity without creating new instance

Posted by: admin June 15, 2020 Leave a comment

Questions:

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>
How to&Answers:

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.

Answer:

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

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

Answer:

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.

Answer:

Try this

public void onBackPressed() {
    super.onBackPressed();
    Log.d("Back", "TEST");
}

And thats all, what you need.

Answer:

Use moveTaskToBack(true);

public void onBackPressed() {
// TODO Auto-generated method stub
moveTaskToBack(true); 
super.onBackPressed();
}

or You can also use finish()