Sachin Kadian
Sachin Kadian

Reputation: 201

how to use onResume activity in android

I am trying to build an application in which I am applying password on application. when the user left the application and open again then I will ask for the password. for example application is running , the user clicks the home button means left the application then he open it again the the application will ask for the password.

I am creating a password dialog in onResume() method of activity. but the problem is that if the user goes to the next screen and comes back to that screen then also onResume() will execute and will ask for the password. but I don't want this. I want password alert should occur only if he left the application and come again. please tell me any solution where should I write that code.

Upvotes: 2

Views: 1092

Answers (5)

baske
baske

Reputation: 1352

It would seem to me that what you are actually trying to keep track of is number of "Started" Activities in your Application: #(Started activities) > 0 would then mean that your application hasn't been put to the background, because if the user presses Home, ALL your App's Activities are bound to have onStop() called.

In order to keep track of the number of "Started" Activities you could introduce a singleton like so:

public class LifecycleTracker {

private LifecycleTracker mInstance;

private int mNrOfStarted;

private LifecycleTracker() {
    mNrOfStarted = 0;
}

public static LifecycleTracker getInstance() {
    if (mInstance == null) {
        mInstance = new LifecycleTracker();
    }
    return mInstance;
}

public void incrementStarted() {
    mNrOfStarted++;
}

public void decrementStarted() {
    mNrOfStarted--;
}

public boolean fromBackground() {
    return mNrOfStarted == 0;
}
}

Then, in all your Activity's onStart() methods you should first check if you are coming from the background:

if (LifecycleTracker.getInstance().fromBackground()) {
    //start authentication
    //don't forget to increment after authentication as well!
} else {
    LifecycleTracker.getInstance().incrementStarted();
}

And in all your Activity's onStop() methods you should decrement:

LifecycleTracker.getInstance().decrementStarted();

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

Assuming you have two activities a(displaying dialog) and b.When you navigate from one activity a to another activity b. Activity a goes to background. activity b is in foreground. both are put to activity backstack. back stack works like LIFO (Last in first out). When you click back button. activity b is pushed from back stack and activity a is displayed. When activity is paused it has to resume. When you activity is stopped onStart() is called and in succesion onResume() is called. So if you calling dialog in onResume() it iwll get displayed.

Have look at the below links.

http://developer.android.com/training/basics/activity-lifecycle/index.html.

http://developer.android.com/guide/components/tasks-and-back-stack.html.

Note Quoted from the above link: Multiple tasks can be held in the background at once. However, if the user is running many background tasks at the same time, the system might begin destroying background activities in order to recover memory, causing the activity states to be lost. See the following section about Activity state.

Edit

You must implement the onCreate() method to perform basic application startup logic that should happen only once for the entire life of the activity. So display dialog in onCreate() not onResume(). Unless activity is destroyed it is not created again.

Upvotes: 0

Chirag Patel
Chirag Patel

Reputation: 11508

You can set variable when home key is pressed

@Override

public boolean onKeyDown(int keyCode, KeyEvent event) {

   if(keyCode == KeyEvent.KEYCODE_HOME)
    {
     fromHome = true;
    }

return false;
};

and set variable false when you navigating from this activity

fromHome = false;

and check flag in OnResume()

Upvotes: 0

a fair player
a fair player

Reputation: 11766

You can create an activity to handle the Authentication process and lets call it (A). and let your current activities be (B) and (C). So, this is how it goes:

1- activity (A) is the launcher.

2- add android:noHistory="true" to the <activity> tag of activity (A) in the manifest.xml

3- from (A) you can navigate to (B) then to (C).

4- from (C) you can navigate back to (B) but you'll not be able to navigate back to (A).

Upvotes: 0

Amitabh Sarkar
Amitabh Sarkar

Reputation: 1311

Give it a try.

Suppose u have A,B activity, create a static variable in A as loggedIn=false;

now suppose u started B from A, on B onbackpress method always make loggedIn=true;

In activity A's on resume method check

if(!loggedIn){ showLogin dialog } then assign true again loggedIn=true;

now if user press home button loggedIn flag will be false and when resumes application the login dialog will be called.

Upvotes: 1

Related Questions