Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can be the possible use case for finishActivity() [closed]

As i already know about Finish() and FinishActivity(). i.e

finish() Call this when your activity is done and should be closed.

finishActivity() is used to finish another activity that you had previously started with startActivityForResult(Intent, int);

i want Briefly about this with sample Example code. For Understanding how it works.

like image 424
R KiranKumar Avatar asked Dec 09 '22 09:12

R KiranKumar


1 Answers

finish() Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

finishActivity(int requestCode) is used to finish another activity that you had previously started with startActivityForResult(Intent, int);

Edit

Like if you simply write finish(); then it just gonna finish your current activity in which you are, but when you use finishActivity(1001) then 1001 is request code which you have passed from startActivityForResult(intent, 1001); so it will finish your that activity which you came from

Example.

@Override 
        public boolean onKeyDown(int keyCode, KeyEvent event) { 
                // TODO Auto-generated method stub 
                if (keyCode == KeyEvent.KEYCODE_BACK) { 
                        finish(); 
                        finishActivity(107); 
                        Intent intent = new Intent(this, Menu.class); 
                        startActivity(inten); 
                        return true; 
                } 
                return false; 
        } 

So above example will finish your current activity as you have written finish(); and also finish your previous activity as we have written finishActivity(107);, where i already told you that 107 is code that you pass from your other activity.

Let's suppose you have 2 activity (Activity A and Activity B), Main Activity A is the Starting Activity. Activity B will run on top of Activity B and is a Blur View. and when i you are redirecting to the Activity B you are not finishing activity A. and you are starting Activity B like this

startActivityForResult(intent, 107); 

so in activity B you have

@Override 
            public boolean onKeyDown(int keyCode, KeyEvent event) { 
                    // TODO Auto-generated method stub 
                    if (keyCode == KeyEvent.KEYCODE_BACK) { 
                            finish(); 
                            finishActivity(107); 
                            Intent intent = new Intent(this, Menu.class); 
                            startActivity(inten); 
                            return true; 
                    } 
                    return false; 
            } 

which will finish your both activity A and B.

Hope this is simple and clear.

like image 173
InnocentKiller Avatar answered Dec 11 '22 09:12

InnocentKiller