0

I have 2 activities, a 4 digit pin style login Activity (MainActivity) and a content Activity.

This app stores private information and such that should not be able to be seen when resuming an activity, therefore i want the app to completely restart on the login activity each time it's launched, rather than picking up where it left off.

I've tried messing around within the content activity's onStop() and onResume methods, but these seem to be highly unreliable as sometimes when i have the onRestart setup to relaunch the login activity, it won't launch it at all, and calling for the login screen to be activated in the onStop() will prevent me from being able to finish up tasks in the background, such as saving data.

Is there anything i can add to the manifest file that will tell the app to restart from the login activity no matter what? One of the worst things that can happen when working on an app like this is that the information is accessible to someone other than the owner that wasn't forced to login..

Zachary Wathen
  • 97
  • 1
  • 2
  • 11
  • I dont think there is any option like that in the manifest...You would need to overide the onStop and onResume methods..and figure out a way to let your app know when it is authenticated.... – Dominic D'Souza Aug 27 '15 at 04:30
  • i'll probably just have to set the onStop method to pull up the loginscreen each time necessary, only issue is that from within my content activity, the user is able to upload a large amount of images to the app which will be stored in the app's internal storage through an AsyncTask, and if the onStop method is telling the app to go to the loginscreen, then if the user stops the activity during the uploading process, then a lot of the data the app keeps track of during the process may be thrown out of shape. – Zachary Wathen Aug 27 '15 at 04:37
  • I don't think you need onStop() to achieve your goal. I think you need to override onResume() and onRestart() only. – iTurki Aug 27 '15 at 04:43
  • you need onPause() and onResume() instead of onStop() and onStart(). – Seshu Vinay Aug 27 '15 at 04:46

2 Answers2

1

What you need is to focus on two calls in your Content Activity:

  • onPause(): This is where you kill Content activity and remove it from the stack. You can do this easily:

    @Override
    public void onPause() {
        super.onPause();
        //Save your data here
    
        finish(); //Kill Contect Activity.
    }
    
  • onRestart(): This is where you redirect the user to the Main Activity.

    @Override
    public void onRestart() {
        super.onRestart();
        Intent i = new Intent(this, MainActivity.class);
        startActivity(i);
        finish(); //Kill Contect Activity.
    }
    

This graph might help you in understanding the Activity lifecycle:

enter image description here

Zachary Wathen
  • 97
  • 1
  • 2
  • 11
iTurki
  • 16,292
  • 20
  • 87
  • 132
  • I implemented those modifications to the content activity's activity (Which extends ActionBarActivity) and the result is that when my login screen attempts to start the content activity upon a successful login attempt, i'm assuming the onResume() is called thus making the login activity start all over without ever actually getting to the content activity. – Zachary Wathen Aug 27 '15 at 20:58
  • @ZacharyWathen You are absolutely right! I didn't pay attention to that. Ok, now try moving the code of `onResume()` to `onRestart()`. It should work, I think. I edited the answer. – iTurki Aug 27 '15 at 21:10
  • for some reason, the onRestart isn't always called with this app. There has been several cases where i've logged in, hit the home button and then came back to the app later only to resume where i left off, instead of the app requiring the login screen. What i've done is added a boolean variable that is only set to true once the content activity has loaded up. (After the onResume is called) called isLoaded. In the onResume, i've set it to only start the loginActivity if isLoaded=true (which it's not when the onResume is called prior to setting up the activity). – Zachary Wathen Aug 27 '15 at 21:30
  • Going ahead and marking this as the solution as this will be the correct answer in most cases. Thanks! – Zachary Wathen Aug 27 '15 at 21:31
0

What you need is setting the flag android:clearTaskOnLaunch and android:excludeFromRecents for your activities in the Manifest.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • I already had android:excludeFromRecents added to my activities and i went back and added android:clearTaskOnLaunch to my activities and that resulted in none of my app's data showing, but rather a blank activity with "hello world". I then tried it in the application tag, but still no luck. I'll dive more into this solution if i am unable to get the ones above to work out. Thanks! – Zachary Wathen Aug 27 '15 at 20:49