Going back and forth between intents without losin

2019-07-12 23:19发布

I have a form. Here, the user fills in details like event name, description, location etc. To select the location, I have set a button. This is my button code:

btnMap.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent map = new Intent(getApplicationContext(), Map2.class);
            startActivity(map);
        }
    });

I select the location from there, and the data is passed back to my main intent correctly. But the thing is, the data in the other fields like event, description etc. that users filled in are lost.

I tried startActivityForResult(map,1); too. Doesn't work. Is there a way to keep the data, without sending them in a bundle to the other intent and getting them back? like the select image button does? It goes to the gallery and comes back without resetting the other fields

contactImageImgView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Image"), 1);
        }
    });

2条回答
狗以群分
2楼-- · 2019-07-12 23:47

You need to save your activity state.

Override the method onSaveInstanceState and persist all your data in the bundle

In onCreate, if savedInstanceState is not null, restore all your data and set all the fields.

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

    savedInstanceState.putInt("MY_FIELD", 43);
    // ... other fields


    super.onSaveInstanceState(savedInstanceState);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first


    if (savedInstanceState != null) {

        int value = savedInstanceState.getInt("MY_FIELD");

        // ... update your views


    } else {
        // no previous state, start fresh
    }

}

Details here

Note: you have mentioned that rotation is blocked. This is generally bad for the user unless it's part of the feature of the app.

Explanation:

A screen rotation is a configuration change that causes the activity to restart with the appropriate resources. There are many more configuration changes such as screen unlock,device docked etc. Not all of them trigger an activity restart but some do. When your activity restarts due to a configuration change, onSaveInstanceState gets called and savedInstanceState will be not-null when onCreate is called.

查看更多
爷的心禁止访问
3楼-- · 2019-07-12 23:47

When you start a new activity Android may destroy the previous one to recover some of the resources it was consuming. In that case, when you return to that previous activity, it is actually a new instance of the Activity subclass and the internal state has been lost. It's up to you to save and restore any state necessary within the onSaveInstanceState and onCreate callback methods, respectively.

Have a look at this page for more information and examples: Recreating an Activity | Android Developers

public class FormActivity extends Activity {

    private static final String NAME = "NAME";

    private EditText nameEdit;
    // Declare other view references here...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form);

        nameEdit = (EditText) findViewById(R.id.name_edit);
        // Find other views...

        if (savedInstanceState != null) {
            final String nameValue = savedInstanceState.getString(NAME, "");
            nameEdit.setText(nameValue);
            // Restore other saved values...
        } else {
            // No saved values to restore
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        final String nameValue = nameEdit.getText().toString()
        outState.putString(NAME, nameValue);
        // Put (save) other values into the outState bundle...

        super.onSaveInstanceState(outState, outPersistentState);
    }

    // Other FormActivity code...
}
查看更多
登录 后发表回答