Reputation: 2584
Consider following situation:
I have an application, which contains some critical information (e.g. banking app). The application itself has multiple screens.
I would like to have the following behavior: Every time after application becomes inactive for any reason (e.g.: user pressed "home" button, user switched to other app using list of recent tasks, user shut down display with power button, input call happened etc...)... so every time after that I want, that when user comes back to the app, the password is requested.
The first solution, which comes to mind is to request password every time in onResume()
. But this of course does not work, because this method is also called, when internal transition from one activity to another within the application happens (and of course, at this point the password should not be requested).
So to solve this I would need to know, if onResume()
was triggered from "internal transition" or from outside the application.
I would be very grateful, if someone could suggest, how to do it.
If there is another solution to the problem, I would also be happy to know it.
Thank you.
Upvotes: 0
Views: 667
Reputation:
I understand what you want to do actually. You didn't specify any programming language. So, I am working with Java
. There's something called sharedPreferences
where you can store data. You have initialize it.
//Declare shared Preference
SharedPreferences pref;
SharedPreferences.Editor editor;
Then, file location
//declare shared prefernce file name
public final String PREFS_GAME="com.istiak.sharedpreference.Game";
Then, mode
//Creating SharedPreferences name game and mode is private
pref=getSharedPreferences(PREFS_GAME,MODE_PRIVATE);
editor=pref.edit();
To save data
editor.putInt("highScore",score);
editor.apply();
To get data
int getSaveScore=pref.getInt("highScore",0);
Here is my git repo
You can save user password on sharePreferences. So, when you is reopening the activity than check getSaveScore
getPassword
(for you) empty or not
Upvotes: 0
Reputation: 3105
I think what you're looking for is ProcessLifecycleOwner
ProcessLifecycleOwner is a special kind of LifecycleOwner that is a composite of all of your Activities. It provides lifecycle for the whole application process
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0"
Create an Application
class and set custom app lifecycle observer
class App : Application() {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(appLifecycleObserver)
}
}
Custom LifecycleObserver
class
class AppLifecycleObserver : LifecycleObserver {
/**
* When application come to Foreground
*/
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onEnterForeground() {
// do something
}
/**
* When application goes to Background
*/
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onEnterBackground() {
// do something
}
}
You check the documentation for detail explanation and this article.
Upvotes: 1