Android Start activity without creating a new instance

Suppose I have activity A, which acts as the root activity for my application. and to form this activity, I move on to activity B.

I want to be able to return from B to Without creating a 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 calls the onCreate call in action A. What I want to do is have A in the background, when activity b is active and when it is finished, switch to activity A

this is a 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> 
+7
source share
5 answers

Depending on the life cycle of the android activity, when you run action A:

List of life cycle methods:

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

Activity Status:

 Activity A : Resumed 

When you run action B now:

List of life cycle methods:

 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 run A again now:

List of life cycle methods:

 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

Here you can find the details.

According to the default behavior, activity A goes into onStop () state and is inactive and needs to create a new instance when returning to activity A. All I know is that there is no way to save instance A,

+11
source
 intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 

Try it ... It makes the old activity frontal without a new instance ...

+3
source

You do not have to do anything. Just remove your onBackPressed() method from Activity B.

By default, when you switch from Activity A to Activity B, Android adds Activity A to the back. When you press the "Back" button from "Activity" B or end it, it automatically restores the action "A" from the back stack.

If you want to end your activity B programmatically by calling finish() when you want to do it.

+2
source

try it

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

And all you need.

0
source

Use moveTaskToBack (true);

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

or you can also use the finish ()

0
source

All Articles