2

I am developing an Android app in which I want to check if the user has minimized the application or just come from another activity.

In detail, if the user have started another app, went to the home screen or locked the screen, I want to show the activity where the user will enter the password to access the app. But where or how to check this exactly?

https://developer.android.com/guide/components/activities/activity-lifecycle.html

I was trying onResume() but according to documentation onResume() can be fired if the user’s navigating to another activity and coming back.

Quintin Balsdon
  • 5,484
  • 10
  • 54
  • 95
xsheru
  • 467
  • 4
  • 25
  • 1
    Possible duplicate of [Difference between onPause and onStop()](http://stackoverflow.com/questions/9266417/difference-between-onpause-and-onstop) – Null Jan 21 '17 at 02:01

3 Answers3

1

I'm not very clear on what you are trying to achieve. The life cycle diagram is quite clear if you are wondering which lifecycle method it would hit when something happens. Basically, it's the same to minimise the app and go to another activity. But if you are referring to coming from another activity in your own app, you can distinguish your own activity by adding extra information to the intent you use.

Basically, it's like this:

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra(key,value);
startActivity(intent);

And in your SecondActivity, you can always retrieve that data like this:

Bundle bundle = getIntent().getExtras();
if ( bundle != null && bundle.containsKey(key) ) {
    value = bundle.getInt(key); // not nessecarily getInt(), you should use according to your value type
    // use the value to tell if it is from your own app
} else {
    // it is not from your own app
}

You can use this mechanism combined with the lifecycle methods. For example, if you use the latter code in your onCreate() method, then whenever the Activity is created, if will check who creates it, which sounds like your what you might want.

  • I want to create a Pin Lock for my app which will lock the app if user minimize the app by going to Home Screen of Android or by opening another app but it should not lock it if in case of... User starts app and HomePage Activity is open now user click on Settings to open Settings Activity. Now if user goes back by pressing back button Settings activity will finish and HomePage Activity will Resume. At this stage I don't want to lock the application because user haven't left the application he is just opening and closing activities. – xsheru Jan 21 '17 at 03:19
  • App should pin locked only when user minimizes the application and starts it again – xsheru Jan 21 '17 at 03:23
  • That's what I thought at first. So when you navigate between your own activities, use the methods I mentioned to create the intent, and in the onResume() method, use the second code block I gave you to tell if it's from your own activity. If not, it must be from other activity, or from a minimised app or reopened lock screen. In this case, you navigate the user to the PIN page again. – CristianoYL Jan 21 '17 at 03:49
0

As soon as your activity becomes visible it will call OnStart() and as soon as it is ready for the interaction(such as touch ,click etc event). it calls onResume, at this stage your app is running and it is completely in foreground. When your activity start another activity or a dialog box then it calls onPause it means activity is visible but user can not interact with the Activity UI. in case we start another Activity which completely hides the previous activity then its onStop method is called

44kksharma
  • 2,740
  • 27
  • 32
0

onPause: Called when another activity comes into the foreground.

onStop: Called when that other activity is completely visible.

onResume: Called when your activity is navigated back to from the onPause state.

Maybe your app was already in the onStop state, so then it would call onRestart.

user2352923
  • 47
  • 1
  • 9