简体   繁体   中英

Save settings button and back to previous activity

I have an activity that has edit text and you can type your name in it, and when you click Save button you will be redirected to MainActivity , but I don't want to open MainActivity by Intent , I make that save button save your name with shared prefs and everything works fine but I don't want to open my Main in Intent I want that when I clicked on save button the current activity that saves data close and previous activity open. sorry for my bad English. this is my code for save button

        submitButt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == submitButt.getId()) {
                String name = inputName.getText().toString().trim();
                if (isValidInput(name)) {
                    Intent setint = new Intent(SettingsActivity.this, MainActivity.class);
                    setint.putExtra("name", name);
                    saveData();
                    startActivity(setint);
                    Toast.makeText(SettingsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show();

                }
            }
        }

Then you just open the PreviousActivity instead of the MainActivity. Add a checker inside the PreviousActivity if "name" has data or not. You can also use finish(); this will kill the activity.

** Update **

In your MainActivity, let's say you have TextView name. Add:

@Override
public void onResume(){
    super.onResume();

    // Check your variable if it has value or none
    textViewName.setText(variableForSharedPref)

}

Then you can proceed with usual process to go in Settings and Go Back to MainActivity.

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