Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does starting an activity from a widget cause my main activity to start as well?

When I start an activitiy from a widget I want the back button to go to the home screen but instead it goes to the app's main activity. After toying around I found that if I somehow close the main app activity, this problem doesn't occur. Strange.

I found a solution here that said to call finish(); in my main activity's onPause(). Obviously this is the wrong solution e.g. reorientation of the screen causes an onPause() so the will activity will die whenever the phone is rotated.

This is how I start my activity:

@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 running the activity differently, but still it doesn't work correctly:

Intent myIntent = new Intent();
myIntent.setClassName("com.emergency.button", "com.emergency.button.EmergencyActivity");
like image 667
ubershmekel Avatar asked Dec 26 '10 22:12

ubershmekel


People also ask

How do I make another activity as main activity?

If you want to make Login activity your main activity then put the the intent-filter tag inside Login activity. Any activity your want to make your main activity must contain intent-filter tag with action as main and category as launcher.

How do I start the same activity again on android?

If you just want to reload the activity, for whatever reason, you can use this. recreate(); where this is the Activity. This is never a good practice. Instead you should startActivity() for the same activity and call finish() in the current one.

How do you find the main activity of an app?

This can be found in the application's manifest. The main activity is the activity with the intent-filter whose name is android. intent. action.


3 Answers

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

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

@Octavian - I should have clarified that I start the activity from an onReceive in an AppWidgetProvider. I'm at the home screen, starting an activity titled B, but somehow both A and B are in the activity stack instead of just B.

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

like image 151
ubershmekel Avatar answered Oct 13 '22 00:10

ubershmekel


Although I've never used widgets, I believe that when you click the widget you are resuming an existing task. Hence, when you are in that task, you will return to the latest activity in that task (instead of Home).

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

like image 34
Pedro Loureiro Avatar answered Oct 13 '22 01:10

Pedro Loureiro


The behavior is not strange it is just the way Android works. The activity stack just keeps track of the all the activities. Now when you start an activity A which starts another activity B then your stack looks like (B, A). If you press the back key then activity B is going to get popped off the stack and A is going to be brought to foreground again.

The right solution is to just call finish() right after firing off the Intent.

like image 25
Octavian A. Damiean Avatar answered Oct 12 '22 23:10

Octavian A. Damiean