-1

I have developed an app that stores all private data information hence I develop password protected screen.

When I open the app the password screen comes up properly but after logging in and if I am editing anything and then I press the home button I have an issue the app reopens to protected screen without the password.

How do I send it back to password screen onResume?

I need to figure out a way to get Home button pressed on Fragment and activity? Or is there a better option?

Can somebody help me figure this out?

Thanks!

Sanjana Nair
  • 2,526
  • 4
  • 25
  • 41
  • What about calling 'finish()' in 'onPause()' ? Or alternatively replace the actual Fragment with the password Fragment in 'onRestart()' ? – momo Jun 11 '15 at 07:14

3 Answers3

2

What happens is that your application get's killed and all the data which was entered is refreshed or removed.

You could use SharedPreference to store data which can be retrieved again at the time of opening the application.

I suggest you to save the user name and password entered onPause() or on onDestroy() and reproduce it back on onCreate() (if any).

Setting values in Preference:

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "xyz");
 editor.putString("password","abc");
 editor.commit();

Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  String password= prefs.getString("idName", "No name defined");
}

EDIT

If you want a particular activity to open first in an application, you need to register it in the AndroidManifest.xml using:

<activity android:name=".put your started activity name here"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Register your password screen here and it will open whenever you open your application.

And if you don't want to change your launch application and just want to send it to another activity, I suggest you to use intents.

Intent i = new Intent(Source.java, Destination.class);
startActivity(i);
finish();
Uma Kanth
  • 5,664
  • 2
  • 18
  • 41
  • Thanks for quick reply. The point here is I don't want to store any data. I want it to go to password screen for me to enter the password instead it opening the screen directly. I am talking about when I click on HOME button and then press to recent button which will have my app when I open it it just opens the protect screen. Which I don;t want. I want to get it to my password screen.. – Sanjana Nair Jun 11 '15 at 05:27
0

When you call onPause and onDestroy, automatically log out of the server with that username + password. And then every time you log on make it check to see if you are connected/logged in to the server, if you are not (Which will always be the case) then switch to the login activity. Hope this helped. :)

Grobbed
  • 167
  • 1
  • 11
  • What happens is when I press Home Button and then click recent button on my phone and then open the app. The protected screen comes up but I need to show password screen? – Sanjana Nair Jun 11 '15 at 05:31
  • Change the activity to the Password screen on every onResume (A method that is called every time the activity is opened) – Grobbed Jun 11 '15 at 23:59
0

You need to override Android inbuild method onSaveInstanceState(Bundle savedInstanceState). This is more easy and work well.

Saving Android Activity state using Save Instance State

Community
  • 1
  • 1
Nandan Kumar Singh
  • 1,069
  • 12
  • 13