2

What happens when finish() method is called in onStop() method?

Does it causes anr : means it calls

onPause()->onStop()->finish()->onPause()....

or it finishes the activity : means it calls directly

onDestroy()

Actually, I want to finish my activity when it is completely invisible.

EDIT:

See this scenario, I launch an activity B whose layout height and width is smaller than activity A, so activity A is partially visible and when I press the home button activity A becomes completely invisible. At this point I want to close activity A, so that it do not call onRestart().

Thanks in advance.

Susomrof
  • 234
  • 5
  • 21
  • 1
    So why not you are adding the code at onPause? – Pankaj Kumar Jan 03 '14 at 13:16
  • System will call onDestroy() method and Activity finishes as usual... – Gopal Gopi Jan 03 '14 at 13:16
  • @PankajKumar what if Activity has a Dialog upon it? and what if finishing Activity in such cases ? – Gopal Gopi Jan 03 '14 at 13:18
  • @PankajKumar As I already mentioned, I want to finish my activity when it is completely invisible, since onPause can be invoked when activity is partially visible. – Susomrof Jan 03 '14 at 13:18
  • @GopalRao can you point out some references too?? – Susomrof Jan 03 '14 at 13:19
  • 2
    @GopalRao I don't think that onPause get called when you show Dialog from that activity. Check that. – Pankaj Kumar Jan 03 '14 at 13:19
  • @PankajKumar thanks for sharing your knowledge and updating me. but this may happen when Activities having theme Dialog.Theme (background Activity is partially visible and goes to onPause() state) – Gopal Gopi Jan 03 '14 at 13:30
  • @GopalRao I am only talking about Dialogs, not the Activity which called as dialog. And yes there is no any perfect way to solve his question, until he let the Android to do his task. Thats what I was telling him. – Pankaj Kumar Jan 03 '14 at 13:34
  • @PankajKumar see my editted ques. I've added a scenario of what I'm talking about. – Susomrof Jan 03 '14 at 13:43
  • @GopalRao see my editted ques. I've added a scenario of what I'm talking about. – Susomrof Jan 03 '14 at 13:44
  • So you want to finish Activity A when the application goes to the background, leaving Activity B on the top of the stack? This sounds very strange. – David Wasser Jan 03 '14 at 17:06

3 Answers3

3

It finishes the activity and onDestroy() is called. If you want to finish your activity when it is invisible then you should call finish() in onStop().

Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
PratikGandhi
  • 173
  • 1
  • 10
0

according to your scenario, maintain one flag in MainActivity indicating that other Activity is launched or not? and make sure yourself to finish MainActivity or not based on that flag ...

this may help you...

public class MyActivity extends Activity {
    private boolean isSecondActivityLaunched;

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

    @Override
    protected void onResume() {
        super.onResume();
        isSecondActivityLaunched = false;
    }

    public void onClick(View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
        isSecondActivityLaunched = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(!isSecondActivityLaunched) {
            finish();
        }
    }
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
-1

It will be best way in your case to call finish() ;

Thanks

Satyaki Mukherjee
  • 2,857
  • 1
  • 22
  • 26
  • may be this will work. but I dont want to close my whole application. – Susomrof Jan 03 '14 at 13:28
  • 1
    In Android finish() method only close the current activity from which finish() method called. If you would like to close whole application then there is another way. Same for System.exit() . It will close the current Activity. If my answer will help you then please support this. Thanks. :) – Satyaki Mukherjee Jan 03 '14 at 13:33
  • No no no! You should never ever call `System.exit()`. There is no reason for that. This is bad advice. Calling `System.exit()` will shutdown your entire process (including all your activities, services, etc.), not just this activity. And Android will think that your process crashed. – David Wasser Jan 03 '14 at 17:03
  • @DavidWasser what would you suggest in the scenario mentioned above in ques ?? – Susomrof Jan 04 '14 at 11:15
  • @Susomrof I can't suggest anything yet, because I don't understand what OP is trying to do. That's why I asked my question in the comments. I'm trying to understand his problem before I suggest anything. – David Wasser Jan 04 '14 at 13:10