Why does starting an activity from a widget also trigger the start of my main activity?

When I launch activity from the widget, I want the back button to go to the main screen, but instead it goes to the main action of the application. After the conversation, I found that if I somehow close the main activity of the application, this problem does not occur. It’s strange.

I found a solution here that said finish () is being called; in my main activity onPause (). Obviously, this is the wrong decision, for example. reorienting the screen calls onPause (), so will activity will burn whenever the phone is rotated.

Here's how I get started:

@Override public void onReceive(Context context, Intent intent) { [...] //new Emergency().emDialog(context).show(); Intent myIntent = new Intent(context, EmergencyActivity.class); // FLAG_ACTIVITY_NEW_TASK is needed because we're not in an activity // already, without it we crash. myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(myIntent); 

You can see the rest of the code at http://code.google.com/p/emergencybutton/source/browse

edit: I tried to start the action in a different way, but still it does not work correctly:

 Intent myIntent = new Intent(); myIntent.setClassName("com.emergency.button", "com.emergency.button.EmergencyActivity"); 
+4
source share
4 answers

Although I have never used widgets, I believe that when you click on a widget, you resume an existing task. Therefore, when you are in this task, you will return to the last activity in this task (instead of Home).

Check out this link and choose the right launch mode for your widget http://developer.android.com/guide/topics/fundamentals.html#lmodes

+6
source

Ok, so I'm not quite sure what happened here, but android:launchMode="singleInstance" in action in AndroidManifest.xml fixed it somehow.

  <activity android:name=".EmergencyActivity" android:launchMode="singleInstance" 

@Octavian - I should have clarified that I am starting with onReceive in AppWidgetProvider. I am on the main screen, starting an activity called B, but somehow both A and B are in the action stack, not just B.

http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

+7
source

The behavior is not strange, this is how Android works. The degree of activity simply tracks all actions. Now, when you start action A, which starts another action B, your stack looks like (B, A). If you press the back key, then activity B will be removed from the stack and A will reappear in the foreground.

The correct solution is to simply call finish() right after disabling Intent .

+1
source

Sometimes it is not possible to use startMode singleInstance in an application for some reason. In this case, you should start your activity and clear the operations stack. You can archive this using flags. There is an example:

 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
0
source

Source: https://habr.com/ru/post/1333422/


All Articles