简体   繁体   中英

Finish, reload activities

I have application where Activity A allows to set some options and starts (after click on button) Activity B. User can do something in B and when he finishes, he has choice (RadioButtons):

  1. repeat - it means the B Activity will run again with the same options (taken from A),
  2. new - it means application finishes B Activity and goes back to A, where user can set options again and start B again,
  3. end - it goes out from application (I suppose it should finish B and then A Activity).

First I have done this way:

        Intent intent = getIntent();
        finish();
        startActivity(intent);

Another way I could use is to clean all parameters in this Activity, but above was more quickly.

Second is just a finish() .

Third is the biggest problem and i don't know how to to this. I tried with startActivityForResult() , onActivityResult() and setResult() but i saw it's impossible to set different results depending on selected RadioButton.

Other method I found is

public static void closeAllBelowActivities(Activity current) {
boolean flag = true;
Activity below = current.getParent();
if (below == null)
    return;
System.out.println("Below Parent: " + below.getClass());
while (flag) {
    Activity temp = below;
    try {
        below = temp.getParent();
        temp.finish();
    } catch (Exception e) {
        flag = false;
    }
}
}

source

But don't know how to put in current Activity.

Could you help me with this?

"i saw it's impossible to set different results depending on selected RadioButton" isn't really clear to me. Anyway, I suggest you what I would do: Activity A starts Activity B with startActivityForResult() and A wait for a bundle coming from B. When you build the exit function from B to A, create a bundle with your result. In A, you will analyze the bundle coming from B and decide what to do with. If you would exit, call finish().

Edit: Ok this could be your case:

Activity A:

    public class XY extends Activity { 
    // you need that as a flag to go back and forth:
           protected static final int SUB_ACTIVITY_REQUEST_CODE_04 = 5337; // choose the number you want...
    int result_from_B; // I usually use an int, choose as you want....
    static final String KEY_FROM_B = "mKey";
    @Override 
      protected void onActivityResult(int result,int resultCode,Intent data) {
        super.onActivityResult(result, resultCode, data);
    if (result == SUB_ACTIVITY_REQUEST_CODE_04) {
            if (data!=null){
                Bundle extras = data.getExtras();
                result_from_B = extras.getInt(XY.KEY_FROM_B);
if (result_from_B==1) {
// do what you want
} else {
// do somthing else...
}
}

To call the Activity B, use this:

Intent i = new Intent(this, 
                    ActB.class);
            this.startActivityForResult(i,SUB_ACTIVITY_REQUEST_CODE_04);

In the Activity B:

public class ActB extends Activity{ 
    protected final int SUCCESS_RETURN_CODE = 1; // you need it to come back
int result; // this is the result to give to activity A
}
// in the function to build the output:
// I suppose you have already put something in result:
Bundle bundle = new Bundle();
            bundle.putInt(XY.KEY_FORM_B, result);
            Intent mIntent = new Intent();
            mIntent.putExtras(bundle);
            ActB.this.setResult(SUCCESS_RETURN_CODE,mIntent);
            finish(); 

This is basic. You can manage also booleans back and forth, arrays, all you want (admitted by a bundle). Just put it in a bundle and go :) Don't forget to declare your classes (activities) in the Manifest otherwise it will throw an exception runtime.

First of all, you need to keep track of the Intent used to call B. Try the putExtra() method of the intent to B. You can also package everything into a Bundle and restore it with getExtras() (if I recall correctly, that's the method name). When on B, read the Intent used to call it and save the parameters. You also need to startActivityForResult(B) for the following to work.

repeat - it means the B Activity will run again with the same options (taken from A),

You probably want to call, from B, Activity B again using the FLAG_ACTIVITY_SINGLE_TOP flag in this case. I assume you don't want to have two B instances. Otherwise, just don't use the flag.

Put the Bundle you received from the Intent (from A) again, and catch it (if using single top) in the onNewIntent() method (or just normally, onCreate, if not single top). It goes like this: B -> B onPause() -> B onNewIntent() -> B onResume().

new - it means application finishes B Activity and goes back to A, where user can set options again and start B again,

Depending on the exact behavior you want, you could call A with FLAG_ACTIVITY_REORDER_TO_FRONT . In this case, you end up with A in the foreground and B in the background (pressing back will go back to B).

Or you could call finish() if you just don't want B anymore, and want to go back to A.

end - it goes out from application (I suppose it should finish B and then A Activity).

Do nothing on B, setResult() to something like "RESULT_FINISH_EVERYTHING", and when taking care of the results in A (override "onActivityResult()", IIRC), finish activity A also.

but i saw it's impossible to set different results depending on selected RadioButton.

You could setResult() depending on thw button checked in the radio button. You can set listeners to the radio group and read which button is selected. See RadioGroup.OnCheckedChangeListener or View.OnClickListener if you need actions for eadch individual radio button.

Really, not too complicated. It just depends on what you want. I can clarify all this if you want.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM