Maha
Maha

Reputation: 459

how can i clear the 3 activities from one activity

i have 3 activity (form 1 ,2 ,3 ) and after the user fill the form 3 and send it, i wanna delete the information from the activity (1 & 2 & 3 ) but AFTER SEND IT IN ACTIVITY 3

(i mean if the user after send it then click back i do not wanna him to find his information in form 1 & 2 & 3

.Intent intent = new Intent(getApplicationContext(), Home.class);
 intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(intent);

does this let my 1 & 2 & 3 activity with no user data when he click back after activity 3 ? can you please help me? ===================Edit===============

someone told me this but i do not get it , can you please help me how can do it

pass a reference to the previous forms in the last one and call clear on any element

Thank you all

Upvotes: 0

Views: 401

Answers (5)

grattmandu03
grattmandu03

Reputation: 1286

You can use the onActivityResult method of Activity class like this:

In the activity 1, start activity 2 with this code :

Intent intent = new Intent(this, Activity2.class);
startActivityForResult(intent, 0);

And in the activity 1 override the onActivityResult method like this :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0 && resultCode == 20) {
        // Activity 2 is finish, so finish activity 1
        finish();
    }
}

In activity 2, do this :

Intent intent = new Intent(this, Activity3.class);
startActivityForResult(intent, 0);

and with this :

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 0 && resultCode == 20) {
        // Activity 3 is finish, so finish activity 2
        setResult(20);
        finish();
    }
}

In activity 3, when the user submit your form use the code below :

Intent intent = new Intent(this, Home.class);
startActivity(intent);
setResult(20);
finish();

PS : I advice you to not use getApplicationContext() but rather "this" or "Activity1.this"

Cheers, Sebastien

Upvotes: 1

Maha
Maha

Reputation: 459

.Intent intent = new Intent(getApplicationContext(), Home.class);
 intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(intent)

Done :) Thank you ALL,

Upvotes: 0

Bharat Sharma
Bharat Sharma

Reputation: 3966

There are two different ways to do that.
  1. You can set result after finishing activities. This will work like chain. for example 1->2->3 suppose you are having finish button in activity 3 to send result to acitivity 1, then you can setResult() from activity 3 in finish button click event and finish your activity by calling finish(). In activity 2 you can receive your data in onActivityResult() there you can check resultcode and save you data, like that you can do from activity 2 to activity 1 also, to send your result.

    setResult(); onActivityResult();

  2. for second method, first you have to set single top in your manifest file for your activity 1 in which you will finally return when you will save all your data from 3.

            android:launchMode="singleTop"
    
          from your activity 3 send broadcast to activity 1.
          to finish all your activity which you have opened after 1 you can use your code.
    
    
     Intent intent = new Intent(getApplicationContext(), Home.class);
     intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(intent);
    

// THIS YOU NEED TO WRITE JUST BEFORE STARTING YOUR FIRST FORM ACTIVITY I THINK IN YOUR CASE JUST BEFORE STARTING ACTIVITY 1.

BroadcastReceiver form_filled = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String received_action = intent.getAction();

        if (received_action.equals("form_filled")) {
            Bundle bundle = intent.getExtras();
            // GET ALL DATA FROM BUNDLE.
        }
    }
};
registerReceiver(form_filled, new IntentFilter("form_filled"));

// THIS YOU HAVE TO DO BEFORE FINISHING YOUR ACTIVITY 3

   Intent temp_intent = new Intent();
    temp_intent.setAction("form_filled");
    sendBroadcast(temp_intent);

 Intent intent = new Intent(getApplicationContext(), activity2.class);
 class_obj_class_name class_obj = new class_obj_class_name(); // FILL YOUR DATA IN CLASS OBJ
 intent.putExtra("class_obj", class_obj); //class_obj IS YOUR CLASS OBJECT IN WHICH YOUR INFORMATION SAVED.
 startActivity(intent);

 // IN ACTIVITY 2 ONCREATE
 class_obj = (class_obj_class_name)intent.getSerializableExtra("class_obj");
 // SAME YOU CAN USE TO PASS DATA IN activity3

hope it will work.. sorry for formatting... :)

Upvotes: 1

NullPointerException
NullPointerException

Reputation: 4008

Do try this..

In your last sending activity,if you want to start another activity or existing activity, then add this

finish(); Intent intent = new Intent(getApplicationContext(), new NewActivityOrExistingActivityName.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);

which will clear all the activities in your previous activity stack

Upvotes: 1

Dinesh
Dinesh

Reputation: 6532

Try this below code for solve your problem :

     Intent homeScreen = new Intent(getApplicationContext(), Home.class);
     homeScreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(homeScreen);

Thanks...!

Upvotes: 1

Related Questions