-2

I have 6 activitys A,B,C,D,E,F.In this A is a login activity after login in A activity i go to the activity chain B,C,D,E,F..In this B,C,D,E,F activitys whenever the user press back button it will go back to to previous activity if user press home button after opening the application it will again ask the password how it is possible..?? help me please

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59

2 Answers2

1

You might keep a boolean condition somewhere in SharedPreference. On user login, set it to true. See the answer here on How can I detect user pressing HOME key in my activity? question to detect if HOME was pressed. If HOME key is detected, set boolean in Sharedpreference to false. Inside onResume of the activity where HOME button is detected, check the variable value from SharedPreferences. If false, then you can open the login activity on top of all others and remove all others in the stack.

You don't need to do anything specific on Back button pressing. Although if you want to detect it, you may override onBackButtonPressed.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
1

Firstly, make your login activity's launchmode singleTask in manifest.

Then in rest of your activity's onsaveInstanceState do the following:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
        Log.d("TAG", "onSaveInstanceState");

        SharedPreferences preferences = getSharedPreferences("pref",
                MODE_PRIVATE);
        if (preferences.getBoolean("app", true) == true) {

            Intent i = new Intent(SecondActivity.this, MainActivity.class);

            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
            finish();
        } else {

            preferences.edit().putBoolean("app", true);
            preferences.edit().commit();
        }
    }
Snehal Poyrekar
  • 735
  • 5
  • 17