Close all the activities in android

Closing All Activities and Launching Any Specific Activity

Often times, most apps have an option where all the activities of the current app are closed and any new specific activity is launched. For example, on logging out of the application, all the activities are closed and login activity is started to allow user to make any new session.

This can be done using few lines code with power of intents like this:

Kotlin

If you liked this article, you can read my new articles below:

Go to Code Line from Logcat Output Line

Let’s start by looking at our typical good’ol logcat window in Android Studio like this: Now, when you get this kind of logs while coding your awesome apps, then your first question would be: Where is the source code line for this log?

April 28, 2018

Percentage Width/Height using Constraint Layout

UPDATE: I’ve started a new articlw series on ConstraintLayout to discuss tips and tricks regularly. Here’s the first article about it. 📚Learning ConstraintLayout — 🚀Live Templates for Rescue 🚒 Save time and improve productivity by using Live Templates for ConstraintLayout

April 27, 2018

7 years experience. 💻 Creator of various Open Source libraries on Android . 📝 Author of two technical books and 100+ articles on Android. 🎤 A passionate Public Speaker giving talks all over the world.

Источник

How to detect Android application open and close: Background and Foreground events.

Dec 17, 2017 · 4 min read

This question seems to come up a lot. Especially if you are just starting out with Android developme n t. It’s simply because there is nothing obvious built into the Android SDK enabling developers to hook into application lifecycle events. Activities and Fragments have all the callbacks under the sun to detect lifecycle change, but there is nothing for the whole application. So how do you detect when a user backgrounds and foregrounds your app. This is an example of how your could detect Application lifecycle events. Feel free to adjust and enhance it to suit your needs, but this idea should be enough to work for most applications with one caveat, it will only work for Android API level 14+(IceCreamSandwich 🍦🥪).

Detecting App Lifecycle Evnts

Backgrounding

ComponentCallbacks2 — Looking at the documentation is not 100% clear on how you would use this. However, take a closer look and you will noticed the onTrimMemory method passes in a flag. These flags are typically to do with the memory availability but the one we care about is TRIM_MEMORY_UI_HIDDEN. By checking if the UI is hidden we can potentially make an assumption that the app is now in the background. Not exactly obvious but it should work.

Foregrounding

ActivityLifecycleCallbacks — We can use this to detect foreground by overriding onActivityResumed and keeping track of the current application state (Foreground/Background).

Читайте также:  4636 android не работает самсунг

Implementing a Foreground and Background Handler

First, lets create our interface that will be implemented by a custom Application class. Something as simple as this:

Next, we need a class that is going to implement the ActivityLifecycleCallbacks and ComponentCallbacks2 we discussed earlier. So lets create an AppLifecycleHandler and implement those interfaces and override the methods required. And lets take an instance of the LifecycleDelegate as a constructor parameter so we can call the functions we defined on the interface when we detect a foreground or background event.

We outlined earlier that we could use onTrimMemory and the TRIM_MEMORY_UI_HIDDEN flag to detect background events. So lets do that now.

Add this into the onTrimMemory method callback body

So now we have the background event covered lets handle the foreground event. To do this we are going to use the onActivityResumed. This method gets called every time any Activity in your app is resumed, so this could be called multiple times if you have multiple Activities. What we will do is use a flag to mark it as resumed so subsequent calls are ignored, and then reset the flag when the the app is backgrounded. Lets do that now.

So here we create a Boolean to flag the application is in the foreground. Now, when the application onActivityResumed method is called we check if it is currently in the foreground. If not, we set the appInForeground to true and call back to our lifecycle delegate ( onAppForegrounded()). We just need to make one simple tweak to our onTrimMemory method to make sure that sets appInForeground to false.

Now we are ready to use our AppLifecycleHandler class.

AppLifecycleHandler

Now all we need to do is have our custom Application class implement our LifecycleDelegate interface and register.

And there you go. You now have a way of listening to your app going into the background and foreground.

This is only supposed to be used as an idea to adapt from. The core concept using onTrimMemory and onActivityResumed with some app state should be enough for most applications, but take the concept, expand it and break things out it to fit your requirements. For the sake of brevity I won’t go into how we might do multiple listeners in this post, but with a few tweaks you should easily be able to add a list of handlers or use some kind of observer pattern to dispatch lifecycle events to any number of observers. If anyone would like me to expand on this and provide a multi listener solution let me know in the comments and I can set something up in the example project on GitHub.

Update: Using Android Architecture Components

Thanks to Yurii Hladyshev for the comment.

If you are using the Android Architecture Components library you can use the ProcessLifecycleOwner to set up a listener to the whole application process for onStart and onStop events. To do this, make your application class implement the LifecycleObserver interface and add some annotations for onStop and onStart to your foreground and background methods. Like so:

Читайте также:  Как отключить интеллектуальный ввод андроид

Источник

Close all the activities in android

Android Sliding Activity Library

Easily create activities that can slide vertically on the screen and fit well into the Material Design age.

Sliding activities allow for you to easily set header content, menus, and data onto a slidable screen. The library currently supports a variety of custom features that allow you to make the screen unique. Currently support are the following:

  • Set the title and have this title shrink into the toolbar as you scroll
  • Set a header image that disappears into the toolbar as you scroll
  • Set colors that will affect the header and status bar color
  • Add a floating action button to the bottom of the header that will animate and show/hide itself at the correct time
  • Disable the header and show only content that is scrollable
  • Works with PeekView out of the box, to provide a «3D Touch» effect on your views. See example usage in the TalonActivity sample.

Include the following in your gradle script:

and resync the project.

Sliding activities are very easy to implement. Here is a simple example:

This will create an activity with the given title, the primary colors, and whatever is included in the activity_content layout.

You also need to reference the activity into your AndroidManifest.xml:

More Details: First, extend SlidingActivity. Instead of overriding onCreate(), instead override init() and set all of your options for the app there. These options include:

  • setTitle()
  • setImage()
  • setContent()
  • setPrimaryColors()
  • setFab()
  • disableHeader()
  • enableFullscreen()

More examples of possible activities can be found in the sample application and code snippets will be shown below.

You can configure the scroller before it’s initialised by overriding configureScroller(scroller)

Most activity options should be implemented inside init(). You can implement setImage() anywhere after init(), but none of the others should be outside of this method.

Setting the title so that it fades into the toolbar as the scrolling occurs is very easy. You can either do:

You can either set a drawable resource id or a bitmap as the image:

When setting the image for the image, there are two options:

  1. Set it inside init()
  2. Set it outside init()

Both of these have very different functionality, so it is important to understand the difference.

If you have a drawable included in your project or already have a bitmap loaded in memory, then it would be best to set the image inside of init(). This will cause the activity colors to change based off of the image and it will show the image when the activity is scrolling up from the bottom.

If you need to load the image from a url or memory, you should not do this on the main thread. This means you need to set it after you’ve already initialized the activity. When doing this, the image will be animated in with a circular reveal animation (for lollipop+ users) or a fade in animation. Also, the activity will not look at the image and extract colors from it. It will instead use whatever colors you’ve set as your primary colors.

Setting content is handled the same way as setting content in a normal activity. You can either pass a layout resource id or a View:

Читайте также:  Где найти смайлики для андроид

After you have set the content, it will be available with findViewById() , same as it would with a normal activity.

The primary color will be used to color the header when no image is present and the primary color dark will be used to color the status bar when the activity has been scrolled all the way to the top of the screen.

One thing to note here, setting an image inside of init() will override these colors. If you wish to continue specifying your own custom colors instead of using the image’s extracted colors, call setPrimaryColors() AFTER setImage().

A floating action button can be shown at the bottom of the expanded toolbar and acted on if you need that for your activity.

When the user scrolls and the header begins to shrink, the FAB will be hidden from view. When the header has returned to its original size, the FAB will be shown again.

If you would like to not show the header on the screen and only have your scrolling content animate up, you can call disableHeader() inside init().

If you would like to not have the scrolling content animate up onto the screen and leave a little bit of extra room at the top, you can call enableFullscreen() inside init(). After doing this, the activity can still be slid down to dismiss it.

This property creates an Inbox style expansion from anywhere on the screen. As with many methods, the parameters are left offset, top offset, width, and height, which describe the size of the box that you want to expand from.

My suggestion: in the SampleActivity.addExpansionArgs(Intent) function, you can see that I pass the expansion parameters as extras on the intent. I would recommend using this method to pass the view from the activity under the SlidingActivity, to the SlidingActivity.

Two themes are included with the library that are specifically created for a SlidingActivity. You can use either Theme.Sliding or Theme.Sliding.Light when registering your sliding activity in the AndroidManifest.xml file. You can also use these themes as a parent to your own custom themes and use those instead if you would like.

Current Apps Using Sliding Activities

If you’re using the library and want to be included in the list, email me at jake@klinkerapps.com and I’ll get your app added to the list!

If you’d like to check out the sample app first, you can download an APK here.

Much higher quality than the GIFs above and more options shown: https://www.youtube.com/watch?v=fWcmy7q09aM

Please fork this repository and contribute back using pull requests. Features can be requested using issues. All code, comments, and critiques are appreciated.

The full changelog for the library can be found here.

Credit to the good folks who work on Android. In the contacts app on Lollipop, there is a quick contact activity that was the base implementation for this library. Check it out here.

About

Android library which allows you to swipe down from an activity to close it.

Источник

Оцените статью