0

i have two activity such as test1 and test2. i want to close test2 activity in test1 activity.i need to close test2 activity in test1 activity button click. how to do it,

test1activitybutton.setOnClickListener(new OnClickListener() {          


        public void onClick(View v) 
        {

         // need to close test2 activity    
        }
    });

i need to close in another class activity as above

sivanesan1
  • 779
  • 4
  • 20
  • 44
  • why you want this explaind in detail. because before open test2 how we close it. and if we open test2 then after moving from test2 we use finish(); for close test2. hope you understand.. – Dhaval Parmar Apr 03 '13 at 05:36
  • What are you trying to achieve with that..? – Pragnani Apr 03 '13 at 05:37
  • you can finish any activity from back stack but if you explain, why do u want this, may be u will get a better solution. – Vivart Apr 03 '13 at 05:39
  • Please look in to Activity Lifecycle : http://developer.android.com/reference/android/app/Activity.html – Dhaval Parmar Apr 03 '13 at 05:40
  • It's possible if your start your next activity from current activity else finishing from any activity outta scope. `getParent().finish()` n `finish()` useful in killing parent and current activity – Vikalp Patel Apr 03 '13 at 05:40

6 Answers6

1

In your test1 activity after class declaration.

Activity t1;

and oncreate of test1 activity

t1=this;

In your test2 activity inside onclick listener

Yourtes1activityobject.t1.finish();
Jai Kumar
  • 920
  • 1
  • 7
  • 14
0

You can't and shouldn't be doing this. At a time, only one activity is on the foreground, and the other activities is paused. So, the background activities are not active, and no code on such an activity can be executed.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
0

It not possible to finish the Activity1 in Activity2.One way is possible before you go to the activity2 you have to destroy the activity1 using finish() .

Yugesh
  • 4,030
  • 9
  • 57
  • 97
0

In test2 Activity you can create public static method(), which contains one operator finish()... Then just call this method from Activity test1

timonvlad
  • 1,046
  • 3
  • 13
  • 31
0

can you try this i am not sure it will work or not.

startActivityForResult(intent, requestCode);//here start activity t2


public void onClick(View v) 
        {

         finishActivity(requestCode);  //it will finish your t2 activity requestCode should be same. 
        }
Monty
  • 3,205
  • 8
  • 36
  • 61
  • first start the activity t2 from t1 and come back to t1 and click and see it is working or not. – Monty Apr 03 '13 at 05:51
0

You probably need no button: if you turn the screen, you will return to another instance of your Activity subclass. :)

Did you see this: Finish an activity from another activity ? Also, Finishing Activity from another activity

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127