4

I have an app that needs to call finish when someone exits its main activity (so i do not want it to be paused), even by pressing home activity has to be finished, to handle this currently i simply call finish() in my onPause() method, since everything is done with fragments it works pretty well and gives no stability issues.

My only problem is that i cannot handle orientation changes since onPause() is called before onConfigurationChanged() (allowing me to disable this behavior when rotation occurs).

I could create a service that handles this but its way to complex.

Any idea?

mmBs
  • 8,421
  • 6
  • 38
  • 46
JohnUopini
  • 960
  • 9
  • 24
  • Do you want to start your app at the MainActivity every time your app is launched? That is if your app is at activity B and the user press the home key, then when the app is launched again, it starts at MainActivity instead of activity B? – Hoan Nguyen Apr 29 '13 at 08:35
  • If `onConfigurationChanged()` is called before `onStop()` you could call `finish()` in it. – rciovati Apr 29 '13 at 09:03
  • @HoanNguyen there is no activity B i have only one activity – JohnUopini Apr 29 '13 at 13:42
  • @rciovati i want to finish() during onPause(), if i finish() on "onStop()" activity won't finish when its just paused. – JohnUopini Apr 29 '13 at 13:42

4 Answers4

7

You can use onWindowFocusChanged event instead of onPause. This function is not called when orientation changed.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    Log.d(TAG, "FOCUS = " + hasFocus);
    if (!hasFocus) finish();
}

But note: this event is called when activity is still visible (like onPause()), you should use onStop if you want to finish the activity when it is really and fully invisible:

private boolean isInFocus = false;

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    Log.d(TAG, "FOCUS = " + hasFocus);
    isInFocus = hasFocus;
}

@Override
public void onStop() {
    super.onStop();
    if (!isInFocus) finish();
}
matreshkin
  • 2,199
  • 18
  • 28
  • Thank you for solving my problem - I wanted to find a way to differentiate app minimization and orientation changes. – John Roberts Oct 17 '13 at 13:30
3

its simple just do:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        display = ((WindowManager) getSystemService(WINDOW_SERVICE))
                .getDefaultDisplay();
        orientation = display.getOrientation();

    }

@Override
    protected void onPause() {
        // TODO Auto-generated method stub

        int orientation_ = display.getOrientation();
        if (orientation_ != orientation) {
            finish();
        }
        Log.e("hello=---->", "onPause");
        super.onPause();
    }
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

From your question, it seems that you only have one activity, in this case you should set a flag in the manifest instead. In your MainActivity manifest add

android:clearTaskOnLaunch="true"
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
0

You could listen for orientation changes, and on the change event store a boolean value about it, like

boolean orientationChanging;

make this true when orientation is changing, and false afterwards, than in your onPause:

@Override
protected void onPause() {

    if(!orientationChanging){
                finish();
            }
}
Analizer
  • 1,594
  • 16
  • 30