Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Start activity without creating new instance

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>
like image 655
Ibrahem Ahmed Avatar asked Apr 09 '13 14:04

Ibrahem Ahmed


2 Answers

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

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

like image 140
Vu Hong Avatar answered Oct 21 '22 12:10

Vu Hong


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.

like image 27
Kaidul Avatar answered Oct 21 '22 13:10

Kaidul