0

I have a react native project. And there is a service running in the background. When the app is not running, the service is still alive, and it may want to start the MainActivity in some time. I’m using the following code to start MainActivity(I tried noFlag/addFlag/setFlag):

Intent Intent = new Intent(context, MainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(key, value);
context.startActivity(intent);

The AndroidManifest.xml is declared like:

<activity
    android:name=".MainActivity"
    android:launchMode="singleTask”>
</activity>

Each time the MainActivity will be created twice. In the first time we can get the extra value, but the second time it will be empty.

How can I make sure the MainActivity will only be created once?

Thanks.

xi.lin
  • 3,326
  • 2
  • 31
  • 57
  • How do you launch the app for the first time? If you are launching the app for the first time from the installer or from an IDE (like Android Studio) you are probably seeing this long-standing nasty Android bug: https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508#16447508 – David Wasser Aug 17 '20 at 14:13
  • What do you want to happen if the app is already running? – David Wasser Aug 17 '20 at 14:14
  • In my case, the activity is created twice in every push. Since it is a singleTask activity, I wish that it should only call `onNewIntent` instead of creating new activity – xi.lin Aug 17 '20 at 15:21
  • Please kill the app (using settings->apps->your app->force stop). Then launch the app from the HOME screen. Then see if you still see this behaviour. – David Wasser Aug 17 '20 at 16:17
  • Launch the app directly will only call the onCreate method once. But it will always become twice if I launch the app through notification. – xi.lin Aug 18 '20 at 05:09
  • Does the app have other activities? or only `MainActivity`? – David Wasser Aug 18 '20 at 07:16
  • Are you testing on real devices, or on emulator? Does this always happen, or only on specific devices? – David Wasser Aug 18 '20 at 07:17
  • Please post the code you use to create the `Intent`, the `PendingIntent` and the `Notification`. – David Wasser Aug 18 '20 at 07:17
  • @DavidWasser Since it is a pure React Native app, there is only one `MainActivity`, which extends `ReactActivity`. I tested in both real devices and emulator. This always happen. The only code I wrote to startActivity is shown in the question. The notification is generated by the third part SDK. I think it also has some logic to startActivity when I click one notification. The question is that why singleTask mode doesn't work as expected. – xi.lin Aug 18 '20 at 10:28
  • Please create an answer to your question and copy your answer into that. Then accept the answer. This will help others who may have this problem. – David Wasser Aug 18 '20 at 13:05
  • 1
    @DavidWasser Done. Thanks for the remind. – xi.lin Aug 19 '20 at 04:59

2 Answers2

1

Just found the problem. My method breakpoint on startActivity worked.

The third party sdk cannot find property receiver, so it send a startActivity call with intent flag FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK, which overrode my singleTask in AndroidManefest.xml.

xi.lin
  • 3,326
  • 2
  • 31
  • 57
0

As you most probably have known, the problem lies with you calling the startActivity twice. I think it would be lovely if you could show the code that calls the startActivity.

I have just now tested that as long as you have singleTask or singleTop as your launchmode, OnCreate wont be called twice. As you can read from here : https://developer.android.com/guide/components/activities/tasks-and-back-stack

the function that would be called is onNewintent and if the putExtra is empty, it indeed can create a NPE

I hope that helps you, Good luck

Tensky
  • 79
  • 6
  • Thanks for your reply. The service is a push service provided from 3rd party vendor. I haven't found where did they call the `startAcitivity` yet... But since I declare singleTask in the manifest file, I was hoping that it will not be created twice. – xi.lin Aug 14 '20 at 15:17
  • according to the article i pasted above, it won`t be tho. It should be that on Create only called once, the rest will only call on OnNewIntent. May i ask, is your onCreate called twice? if so, does the screen seems like its being created again? Thanks for replying. hope it helps – Tensky Aug 14 '20 at 18:22
  • Yes, I added log in onCreate and onNewIntent, I can see that after onNewIntent is called , onCreate is called again... At the same time, my component in react-native side ran through the componentWillUnmount method. – xi.lin Aug 17 '20 at 02:43