0

i have a android application in which i have 4 activities, A,B,C and D

In Activity A is my login screen

if user is in B,C or in D and if user presses home button from B,C or D the app is getting closed and by default android home screen will come and if they again open that app,those only that Activity is opening from where they press Home button.

I want them to redirect to login page.

Note:i cant use onpause() and onstop() because i am moving from one activity to another continuously.

How should i do that ??

abh22ishek
  • 2,631
  • 4
  • 27
  • 47

3 Answers3

1

Use android:noHistory="true" flag in the manifest for activity b, c and d. This will force the app to show the main activity when one of these activities were visible when user pressed the home button.

user3649706
  • 43
  • 1
  • 6
0

write this code onPause of the activity

ActivityManager activityManager = (ActivityManager)getBaseContext()
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
        String packageLaunched = runningTasks.get(0).baseActivity.getPackageName();

        if (!packageLaunched.contains("yourPackageName")) {
               ///// some constant == true
        }

This will let you know that you moved out of the app!
And there you will keep a constant flag.

Then onResume you should again check for the flag, if its true (i.e. user pressed the home button) re-direct the user to the whatever page you wish to.
I hope you get the point.

Cheers :)

Salmaan
  • 3,543
  • 8
  • 33
  • 59
0

Here are some possible workarounds. Option 1:

An option might be to keep an ActivityStateManager.xml file saved in you app folder, which you will read when starting app. In this xml file you could keep a basic property StartLoginAcivity = true/false.

Create a MainActivity which will be extended by all our activities, except B,C,D and override the home button and set StartLoginAcivity property in ActivityStateManager.xml to false.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {           

    // set StartLoginAcivity property in ActivityStateManager.xml to true    
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

Override the home button in activities B, C, D (or in a RequireLoginActivity which will be extended by these activities), and set StartLoginAcivity property in ActivityStateManager.xml to true.

When starting the app, read ActivityStateManager.xml and if StartLoginAcivity = true, open LoginActivity.

Option 2:

Override the home button in activities B, C, D (or in a RequireLoginActivity which will be extended by these activities), and force close app like How to close an android application?. When starting app, it will open your default main activity

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {

    // force close app

     Intent homeIntent = new Intent(Intent.ACTION_MAIN);
     homeIntent.addCategory( Intent.CATEGORY_HOME );
     homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(homeIntent);
  }

      return true;
            //return super.onKeyDown(keyCode, event);
    }
Community
  • 1
  • 1
A B
  • 338
  • 3
  • 10
  • if i am not wrong -this onKeyDown() will not work on higher versions of android . don't you think so?? – abh22ishek Feb 03 '15 at 06:23
  • It seems onKeyDown still exists in android API 21. http://developer.android.com/reference/android/view/KeyEvent.Callback.html – A B Feb 03 '15 at 11:55