Android - Resume an activity from other application

2.3k views Asked by At

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,

4

There are 4 answers

2
Nick Cardoso On BEST ANSWER

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);
    } 

}
2
Sergey Shustikov On

Intent is powerful mechanism in Android that allows to you start Activities from another process.

You just need setup package and class name. That's all.

For example :

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyExampleActivity"));
startActivity(intent);

Also you may need an singleInstance | singleTask launch mode of your Activity A3.

When you need to launch A3 you need setup FLAG_ACTIVITY_REORDER_TO_FRONT to your Intent and A3 will be reordered to front.

How to make IRC in Android : read here

0
Pranay Narvankar On

When redirecting to B1 -> B2 call finish(); in activity B1..

0
AudioBubble On

first to get from B2 to B1 you need this

Intent intent = new Intent(this, B1.Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("fromB",true);
startActivity(intent);
finish(); 

when You are in B1 one in the onCreate put this

 Bundle b = getIntent().getExtras();
 if(b.getBoolean()){
   Intent intent = new Intent(this, A3.Class);
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   intent.putExtra("fromB",true);
   startActivity(intent);
   finish(); 
 }

I think this will help :D