简体   繁体   中英

How to back from the activity without button

When I was browsing the slack application I found new feature I liked, which is the return of activity by dragging without pressing the back button as usual.

So is there a specific code to do that? I did a research on how to do that but I couldn't find any explanation for it, so how can I do that.

Here is a short video about the new feature click here

Thanks to Coding in flow for that. Watch this tutorial for more explanation.

Follow these steps to do the job.

1- We will use a library to do that called Slidr , use the last version.

// swipe the Activity to close
implementation 'com.r0adkll:slidableactivity:2.1.0'

2- Add a new style in your styles.xml called SliderActivityTheme . This theme will make the background for the activity when swipe it as transparent to show the content of the fragment...etc, "default background white"

<!--Theme for Slider Activity-->
<style name="AppTheme.SliderActivityTheme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

3- A- Add slide method to your class and called in OnCreate

// Make slider on the Activity
public void Slider () {
    Slidr.attach(this);
}

B- Called in OnCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity_name_here);

    // slider the activity
    Slider();
}

or just simply add this in your OnCreate directly

// slider the activity
Slidr.attach(this);

4- In your AndroidManifest.xml add the theme that we had to create.

android:theme="@style/AppTheme.SliderActivityTheme"

Make it like that

<activity
   android:name=".Activity.ActivityImages.ImagesMorningActivity"
   android:launchMode="singleTop"
   android:theme="@style/AppTheme.SliderActivityTheme"/>
<activity

That's all, and enjoy with your new feature;)

Working with Fragment? No problem, check the library page to know how to do that

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