简体   繁体   中英

Returning to home screen from Activity instead of parent

I have a music player activity which doesn't behave as I want. This activity can be opened from inside the app, from notifications bar and when switching/resuming app from background.

When it is launched from the app -> backPress on activity -> returns to previous app activity. OK

When launched from notification -> backPress on activity -> returns to home screen (it is OK)

When resumed from homeScreen/recent apps -> backPress on activity -> returns to home screen (not OK) - user assume to get back to the app since this activity is a leaf and a tab activity is the root activity.

I want to get back to the parent activity when pressing back, not going on the home screen(when resumed from notifications, it is OK if returns to home screen, but both variants are OK for me in this scenario)

<activity android:name=".player.PlayerActivity"
                    android:configChanges="keyboardHidden|orientation"
                    android:label="@string/audio_player_activity_title"
                    android:launchMode="singleInstance"/>

and starting the activity:

//this intent is started from a fragment (SherlockFragment)
Intent i = new Intent(getActivity(), PlayerActivity.class);
startActivity(i);

NOTE: I want single instance to avoid 2 activities of the same type running on screen (happen when using notifications)

Could someone help me with this?

When you use the launchMode: "singleInstance", your activity will be launched in a new Task: [http://developer.android.com/guide/topics/manifest/activity-element.html][1] That's why you dont see the normal behavior of back stack. To be sure that android instanciates once your activity you can set the flag : FLAG_ACTIVITY_REORDER_TO_FRONT when you launch your activity

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM