0

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

Maha
  • 459
  • 1
  • 6
  • 22
  • no that flag won't really help you, do you need to go back to 1, and 2 or can you just clear them form the stack? – MikeIsrael May 07 '12 at 08:03
  • if the user not yet press send in form 3 then i do not want to clear 1 & 2 but if he press send in form 3 then my app will move him to my home and i want to clear 1 & 2 & 3,how can i do it – Maha May 07 '12 at 08:23
  • check my 2nd approach http://stackoverflow.com/questions/10428197/clean-stack-and-exit-app-onbackpressed/10428358#10428358 – Vivek Kumar Srivastava May 07 '12 at 09:12
  • @Venky please engilsh i am not get it, Thanks – Maha May 07 '12 at 09:52
  • @VivekKumarSrivastava i do not have pressBackButton function,should i add it? or the permission is enough?i mean i wanna put just the premisson AM i right? – Maha May 07 '12 at 09:56
  • add activity pressBackButton method. this is activity override method – Vivek Kumar Srivastava May 07 '12 at 11:01
  • @VivekKumarSrivastava should i put it in all activities?also what should i put inside pressBackButton? – Maha May 07 '12 at 11:06
  • can you read my answer properly?. Whenever you want to close application then override the activity onBackPress() method and write the code which mention in my answer – Vivek Kumar Srivastava May 07 '12 at 11:09
  • 1
    http://stackoverflow.com/questions/10379134/finish-an-activity-from-another-activity/10379507#10379507 – MKJParekh May 07 '12 at 14:31
  • @VivekKumarSrivastava i do not wanna close the app, it is just clean the form,but the user still in the Home activity – Maha May 07 '12 at 14:48
  • 1
    Okay I understand. You can use static flag to identify form have been sent or not. After sending form successfully you can change the flag value and according to flag value reset the form data – Vivek Kumar Srivastava May 08 '12 at 05:39

5 Answers5

1

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...!

Dinesh
  • 6,500
  • 10
  • 42
  • 77
  • yes please?becuase right now my sdk is not working and i should submit the code after 2 hours, so i need to be sure that my code is correct,Thank you for helping me – Maha May 07 '12 at 10:13
1

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

grattmandu03
  • 1,266
  • 2
  • 13
  • 32
  • you mean if i finish fill activity 1 then move to 2 then i should finish activity1? i do not wanna that, WHAT I WANt:when the user press button send in activity 3 , activity 1 & 2 & 3 will be finish, – Maha May 07 '12 at 09:42
  • No, I mean when you will finish activity 3, it calls onActivityResult method of activity 2 (which is still alive because you never call finish() method before) where you can call finish() method. This calls onActivityResult method of activity 1 where you can call finish() method. My answer do what you want : when you start your Home intent in activity 3, activity 1 & 2 & 3 will be finish. – grattmandu03 May 07 '12 at 09:57
  • If you use Eclipse, just tape "onActivi" then press ctrl+space (automatic indentation). Or just copy my code above in yours activities. – grattmandu03 May 07 '12 at 11:31
  • but i do not wanna to finish activity 1 & 2 until the user submit, is the code doing this? – Maha May 07 '12 at 12:03
  • Yes the code is doing this (of course if you don't call finish() method somewhere else). If you are on activity 2 and you want to modify some datas in activity 1, just press back button or apply return action on an another button and your activity 1 is still here. – grattmandu03 May 07 '12 at 14:30
  • so should i put onActivityResult with finish inside it in activity 1 & 2 & 3 even if i want to finish it after activity 3? – Maha May 07 '12 at 17:40
  • i tried it but when i press back button , it show me the information inside act 1 & 2 & 3 , should i call the onActivity or not?because i do literally your code – Maha May 07 '12 at 18:17
  • You have to put onActivityResult method in activity 1 and 2. I have edited my answer above with detailed code to put in each activity. – grattmandu03 May 09 '12 at 07:31
1

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

NullPointerException
  • 3,978
  • 4
  • 34
  • 52
  • also clear the data inside the activity 1 & 2 & 3 ? even if the user press Back Button? – Maha May 07 '12 at 09:57
  • my code `Intent i = new Intent(activity3.this, Home.class); startActivity(i);` should i replace it with this `new Intent(getApplicationContext(), new NewActivityOrExistingActivityName.class);` or just add `intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent);` to my code? – Maha May 07 '12 at 10:00
  • just in between these two lines add i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); – NullPointerException May 07 '12 at 10:02
  • if the user press send, then my application will move user to Home page,but when he press back,my application go back to activity 3 with user information, this is my problem – Maha May 07 '12 at 10:02
  • i want after user press send in activity 3 , then act 1 & 2 & 3 will be clean, even if user press back – Maha May 07 '12 at 10:03
  • should i put it in activity 3 ? it will clean 1 & 2 & 3? if i put it in activity 3? – Maha May 07 '12 at 10:06
  • Ok then put 1 more line above the code finish(); this will finish your current activity then you call above code.. it will work – NullPointerException May 07 '12 at 10:11
  • i do not get it, please can you edit your answer,so i can get it,Thank you – Maha May 07 '12 at 10:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10963/discussion-between-rashmi-and-maha) – NullPointerException May 07 '12 at 10:19
1
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... :)

Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
  • can you please tell me please what is the code for send Broadcast that i should send it to activity 1 from activity 3, and how can i receive it in activity 1, and where should i put it ? inside on create or in another function – Maha May 07 '12 at 09:40
  • i am sorry for bothering you but i have all my data in class from act1 & 2 and i call it in act 3, so what it is the data that i should get it from bundle – Maha May 07 '12 at 10:15
  • when you move from 1 to 2 and 2 to 3 save data and pass it to next activity. In this way you will have all your data in activity 3 then you can finish. – Bharat Sharma May 07 '12 at 10:26
  • i put all data in class and call the object i do not pass data between the activitis it all in class, activity 1 & 2 fill the class and activity 3 call the object and show the user information that he fill it in 1 & 2, so how can i use your code – Maha May 07 '12 at 11:01
  • you want to display all your data in activity 3 to users – Bharat Sharma May 07 '12 at 11:05
0
.Intent intent = new Intent(getApplicationContext(), Home.class);
 intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(intent)

Done :) Thank you ALL,

Maha
  • 459
  • 1
  • 6
  • 22