简体   繁体   中英

How to use onBackPressed in an activity that swipes the screen with another activity?

I've a little problem. I've two activities (GalleryActivity and GalleryVideoActivity) and after swiping between them, i would to come back to the first activity (GalleryActivity) pressing just one time on back button because i've to press back button as many times as i swipped. Is it possible? Thanks in advance to everyone!

So if i understand your issue is that you are moving between activities and everytime you move they create new instances so as a solution to suggest is as following, in your manifest file, in activity section add launchMode="SingleInstance", as this would create only one instance of that activity.

  • This is an example
  <activity
            android:name=".ui.apppassword.PasswordRestoreActivity"
            android:screenOrientation="portrait"
            android:launchMode="singleInstance"/>

In GalleryVideoActivity override onBackpress method and navigate back to the GalleryActivity by putting simple intent.

Here is what you need to add to GalleryVideoActivity in order to implement onBackPressed as Mike stated:

@Override
public void onBackPressed() {
    Intent toGalleryActivity = new Intent(this, GalleryActivity.class);
    startActivity(toGalleryActivity);
}

This starts GalleryActivity everytime the back button is pressed in GalleryVideoActivity.

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