Search code examples
androidonresumemultitasking-gestures

Android - Resume an activity from other application


I have two apps A(activity A1, A2, A3) and B(activity B1, B2). My process like this:

A1 -> A2 -> A3 -> B1 -> B2

My question is: from activity B2, how to resume to the existed activity A3 - no creating a new activity A3 - like switching 2 applications by using multi-task button?

Thanks,


Solution

  • You need singleTop to make the activity always use the same instance, then in that activity onNewIntent will be triggered whenever we return there from another activity (via intent)

    <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="..." >
    <application ...>
        <!-- android:launchMode="singleTop" makes sure we reuse the same instance -->
        <activity android:name=".A3Activity" android:label="@string/app_name"
            android:launchMode="singleTop">...</activity>
        ...
    </application>
    
    
    public class A3Activity extends Activity {
        @Override
        protected void onNewIntent(Intent intent) {
            //This is triggered onyl when re-launched
            super.onNewIntent(intent);
            //do anything new here
        }
    }
    
    public class B2Activity extends Activity {
    
        public void someMethod() {
            //This relaunches the A3 activity from same app
            //Intent intent = new Intent(this, A3Activity.class);
    
            //This does it from the other app
            Intent intent = new Intent(
            intent.setComponent(new ComponentName("com.anh", "com.anh.A3Activity"));
            startActivity(intent);
        } 
    
    }