1

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,

Anh-Tuan Mai
  • 1,129
  • 19
  • 36

4 Answers4

2

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

Community
  • 1
  • 1
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • I can launch an new app B (act B1) but I don't know how to resume app A (act A3) ? I'm using: Intent launchIntent = getPackageManager().getLaunchIntentForPackage("org.example"); startActivity(launchIntent); Can you precise? Thanks, – Anh-Tuan Mai Aug 10 '15 at 12:18
  • Thanks you very much. I'm using Messenger to exchange data between 2 apps. Can I do somethings with IPC here? – Anh-Tuan Mai Aug 10 '15 at 12:30
1

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

}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
0

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

-1

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