1

I'm sorry for the title, I can't find a better way to describe my problem briefly. Here is my scenario. There are two apps, A and B. App A has one activity A1, and app B has three activities B1, B2 and B3.

Activity B1 is transparent and works as a proxy, I define intent filters for B1 so A1 can fire implicit intents to B1. Depending on intents' extras, B1 then will fire an explicit intent to start either B2 or B3, then finish itself. B2 and B3 don't have any intent filters and are supposed to be triggered by explicit intent from B1 only. The reason for B1/B2/B3 design is because

  • I own app B, but not A so I can't change the way A fires intents
  • I want to improve app B's UX with multiple activities instead of a single activity

Here is the 'proxy code' in activity B1

public void onStart() {
    super.onStart();

    // useB2() is some helper method that looks at intent's extra
    // and decides if we should start B2 or B3
    boolean useB2Activity = useB2(this.getIntent());

    Intent intent = (Intent) getIntent().clone();
    intent.setClass(this, useB2Activity ? B2.class : B3.class);
    startActivity(intent);

    finish();
}

Problem: If I go to app B the first time from app A, I can see B1's onStart() is called and then it starts B2 (for example). After that, whenever I go to app B from app A, B1's onStart() is NOT called, and B2's onResume() is called directly. For some reason, all subsequent intents fired from A1 does not go to B1. Instead it resumes B2 (or B3 depending on which one is created the first time when B1's onStart() is called). I don't know what's wrong here. Please help. Thanks.

hixhix
  • 771
  • 7
  • 23

1 Answers1

0

Let analyse your code:

Intent intent = (Intent) getIntent().clone();//intent -->B1
intent.setClass(this, useB2Activity ? B2.class : B3.class);
//now your intent-->B2/B3
startActivity(intent);//-->start B2/B3
finish();//Finish B1 but B2/B3 is still alive

Now if you call B from A--> It go to B2/B3 onResume because B2/B3 is alive!

If you want to start B1 everytime, modify lanchMode in xml manifest.

<Activity B1....
     android:clearTaskOnLaunch="true"
     android:launchMode="singleTop"
>

If you want to call some method again in the n_th use, you should move codes from onStart() to onResume().

public void onResume() {
    super.onResume();

    // Your code here!
}
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
  • Why should it work? Anyway I tried and did not address the issue here. – hixhix Nov 10 '15 at 23:46
  • Because as you said, "whenever I go to app B from app A, B1's onStart() is NOT called, and B2's onResume() is called directly". This is Android activity life cycle! If you open app B for the second time, it go to `onResume()` if app B is not yet destroyed!!! – T D Nguyen Nov 11 '15 at 00:51
  • App B has multiple activities. I meant when I go to B1 activity (per intent that A fires), it doesn't go to B1 and goes to B2 instead. – hixhix Nov 11 '15 at 00:56
  • When I call B from A, A always starts an intent whose filter registered to B1. So shouldn't B1 be called instead of B2? Anyway, more analysis is at http://stackoverflow.com/questions/33642152/intents-do-not-go-to-an-activity-that-starts-another-activity Can you please take a look. Thanks. – hixhix Nov 11 '15 at 01:12
  • Hey! I suggest you not finish B1 (remove `finish()`) and try it again to see what happen. – T D Nguyen Nov 11 '15 at 01:18
  • I tried and it didn't work. Thanks for your help though. – hixhix Nov 11 '15 at 01:41