StartActivity does not start the correct activity

corwin42

Expert
Licensed User
Longtime User
I have an App with some activites and a widget. In the widget there are some controls which start different activities of my app on clicking them.
This works, if the app is currently not running (I left the app with the back key). Then the app starts with the correct activity.

If the app is running in the background (I left the app with the home key) then if I start the activity from the widget the activity I specify in StartActivity() is not started but the app is just brought to front with the last activity I used.

Is there any way to force StartActivity() to bring the specified activity to front?

In the documentation there is a note that StartActivity() should not be called from a service. Is this info up to date? Since it is the only way to open the app from a widget I think.
 

corwin42

Expert
Licensed User
Longtime User
Ok, created a small project and found out some more info.

The problem seems to be the activity that is the first on the activity stack. This one is never brought to front again.

To reproduce it:

- Install the app.
- exit app with back key (so no activity is on the stack)
- add the widget AATestWidget to the homescreen
- Press button Act2 to start the Activity 2 (green)
- Press Home to return to Homescreen
- Press button Act3 to show Activity 3 (red)
- Press home
- Press button Act2

Activity 3 is then shown.

If you always use the home button to return to the homescreen you can switch between Main and Activity3 from the widget, but Activity2 does not show.

If you start with another activity so this one is the first on the stack then you can't return to this one with the widget.

Perhaps this is a Android problem? I currently Use Gingerbread 2.3.3
 

Attachments

  • StartActivityProblem.zip
    9.1 KB · Views: 281
Upvote 0

corwin42

Expert
Licensed User
Longtime User
Thanks for checking it.

It's really strange. I would call it a bug (in Android). I think I will search if someone else had this same issue.

Did you try different launchModes (others than singleTop)?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not really a bug. This is the default "launcher" behavior.

I didn't try different launch modes, only different flags in the intent. I haven't tried FLAG_ACTIVITY_MULTIPLE_TASK which might work, as it will probably have other unwanted side effects.

You can workaround this issue in your application. Check the value of Activity.GetStartingIntent in Activity_Resume and start the correct activity (I haven't tested it...).
 
Upvote 0

Inman

Well-Known Member
Licensed User
Longtime User
Not sure if this is the same but I had a similar issue with an app that has 2 launcher shortcuts and sometimes one shortcut opened the other activity. I overcame it by modifying android:launchMode in Manifest file to "singleInstance", for the activity that was not getting launched correctly.
 
Upvote 0
Top