0

Does anyone know how to close an activity from other activity?? for example: i have 3 activity (activity A, B, and C) and from activity C, i can close an activity A.. my activity structure is activity A -> activity B -> activity C how to close an activity A from activity C?

i was try this code :

@Override

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent goToLockScreen= new Intent(this,LockScreenForm.class);
        startActivity(goToLockScreen);
        finish();

but that code is only fot closing activity A from activity B, and can't close activity A from activity C direcly..

Does anyone know about closing an activity direcly from other activity?? thanks..

Sumit
  • 736
  • 8
  • 26
Michael Frans
  • 613
  • 3
  • 23
  • 49

5 Answers5

1

try It work perfectly for me

   `public class aActivity extends Activity {

    public static Activity handleToClose;

    @Override
          public void onCreate(Bundle savedInstanceState) {
    .
    .
    .
    handleToClose = this;
    }

    public void onClick(View v)
    {
   Intent i = new Intent(this, act2.class);
   startActivity(i);
     }
    }`

Now You have to finish Activity-A from Activity-B

Activity-B or 2nd Activity

    `public class act2 extends Activity {
 public void onCreate(Bundle savedInstanceState) {
      // your code here
          }

     public void onClick(View v)
     {
    aActivity.handleToClose.finish(); //this will finish aActivity (1st Activity)
     finish();//to finish current Activity
     }
      }`
Zeeshan Chaudhry
  • 842
  • 2
  • 15
  • 31
1
First Go to parent activity by starting it
@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
switch(Code){
 case A: go to that activity and finsih() this again come back to parent activity
 case B: go to that activity and finsih() this again come back to parent activity
/////and son on
}
Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
  • actually i'm using this code from service.. i create an security lockscreen app.. i must close my lock screen activity from my service when my service receive a unlock command.. any idea? – Michael Frans Aug 03 '11 at 09:01
  • I think you have to register a broad cast, and when your service receive unlock command then broadcast Receiver's on receive method will call from where you can go to your activity to close it – Tofeeq Ahmad Aug 03 '11 at 09:24
  • yes i do.. i try to open that lockscreen activity and finish() it, but the earlier activity is still in there.. im now confusing how to close that activity from my service.. – Michael Frans Aug 03 '11 at 09:41
  • ok if you want to close all activity then simple go to top and then use FLAG_ACTIVITY_TOP_CLEAR, when you go to root activity then you can finish this also ,that way all activities will be finish; – Tofeeq Ahmad Aug 03 '11 at 09:53
  • thanks for your advice, but can u give me an example how to use FLAG_ACTIVITY_TOP_CLEAR?? – Michael Frans Aug 03 '11 at 09:59
  • i was trying this code, but it not works for me: Intent myIntent = new Intent(ListenSMSservice.this,LockScreenForm.class); myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(myIntent); – Michael Frans Aug 03 '11 at 10:01
  • Intent intent1=new Intent(this,Doctors_Clinic_List.class); intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent1);But if you are starting activity from broadcast receiver then you need proper context – Tofeeq Ahmad Aug 03 '11 at 10:25
0
Intent goToLockScreen= new Intent(this,LockScreenForm.class);
goToLockScreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

This is a prescribed way and you should follow it.. if you want some other behaviour yo can implement it.. there are lot many questions asked on this topic.. refer other questions...

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • This will clear all activites but C, as i understood he needs to clear A but keep B and C running, correct me if I'm wrong. – iDroid Aug 03 '11 at 08:32
0

What about starting both B and C forResult and send a result back to pervious activity to let A finally call finish()? Like this:

A startActivityForResult() -> B startActivityForResult()-> C
C setResult()-> B onActivityResult(){setResult()} -> C onActivityResult(){finish()}

Sounds complicated, but maybe it can be used as a workaround?

iDroid
  • 10,403
  • 1
  • 19
  • 27
  • actually i'm using this code from service.. i create an security lockscreen app.. i must close my lock screen activity from my service when my service receive a unlock command.. any idea? – Michael Frans Aug 03 '11 at 08:54
0

try this

If the update activity is launching another installation activity, then you may want to override void onActivityResult(int requestCode, int resultCode, Intent intent) in the update activity, providing the following implementation. Also, when the update activity launches the installation activity, it should do so with startActivityForResult(Intent, int), not with startActivity(Intent).

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
super.onActivityResult(requestCode, resultCode, intent);
finish();
}
kannappan
  • 2,250
  • 3
  • 25
  • 35