How to go from an Activity "A" to an Activity "B" to select data and return to Activity "A" without the editText of Activity "A" being erased?

3

I have the need in an "Activity A" to call an "Activity B" to select data and send it to the "Activity A", but in the "Activity A" I have 10 edit text that when making those transitions with intents they are deleted, and I do not want to pass all the data from one activity to another to return them again.

the calls to the activitys I do with common intents and sending data with bundle, here an example from the "Activity B"

bundle.putString("Location", mList.get(position));
bundle.putInt("Type",3);
Intent intent = new Intent(mContext, ActivityA.class);
intent.putExtras(bundle);
mContext.startActivity(intent);
    
asked by HFFY 20.06.2018 в 22:51
source

3 answers

2

You should not regularly perform an Intent to "return" to an Activity unless you have implemented loading the data from a persistent source, such as a database or file.

What you want to do can be achieved by simply adding the onBackPressed() method to your Activity B:

@Override
public void onBackPressed() {
    super.onBackPressed();
    this.finish();
}

In this way, by clicking on the "back" button of your device, Activity B is closed and you return to Activity A, which was previously loaded and you do not have to reload it.

If you want to send data from Activity B to Activity A then you can do it using a bundle with the data and these are sent in a Intent , you can review this question:

Send data between activities

Send the data in the bundle and send it in Intent :

bundle.putString("Location", mList.get(position));
bundle.putInt("Type",3);
Intent intent = new Intent(mContext, ActivityA.class);
intent.putExtras(bundle);
mContext.startActivity(intent);

To receive them in Activity A, you can do it within the onCreate() method:

 Bundle parametros = this.getIntent().getExtras();

 String datos = "";
 int tipo = 0;
 if(parametros !=null){
    String datos = parametros.getString("Location"); 
    int tipo = parametros.getInt("Type"); 
 } 

and later these values can be added to your EditText , for example:

editTextDatos.setText(datos);
editTextDatos.setText(String.valueOf(tipo)); //* Los valores int debes convertirlos a String para usar el método setText()
    
answered by 21.06.2018 в 01:28
0

You must save the status of your activity. use the method override SaveInstanceState and save all your data. In onCreate , if savedInstanceState is not null, restore all your data and set all the fields.

Android studio concept (Save the status of your activity): When your activity starts to stop, the system calls onSaveInstanceState () so that your activity can store the status information with a collection of key-value pairs. The default implementation of this method stores information about the status of the activity view hierarchy, such as text in an EditText widget or the scroll position of a ListView.

@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
    }

}

More details: Here

    
answered by 20.06.2018 в 23:01
0

Well after finding out more and thinking I found a solution a few days ago but I forgot to publish it I publish it for those who are ever in the same situation or a similar one What I did was very simple, the variables that I needed to capture a value in Activity B are declared as "public static" variables from Activity A, so that in this way I can change the values from Activity B (or any other activity ) and no longer depend on a Bundle to pass the data between Activitys, the next thing I did following Jorgesys' advice was to call the .finish () method; so that Activity B ends, returning to Activity A without losing any data from the editText and having changed the values of the variables that it needed. Below I leave an example in code:

Activity A:

public static int variableA;
public static String stringB;

Activity B:

import static  miProyecto.ActivityA.variableA;
import static  miProyecto.ActivityA.stringB;

/*clase y metodos...*/
.
.
.

variableA=3;
stringB="nuevo valor";
this.finish(); //o puedes utilizar el metodo onBackPressed();

It is worth emphasizing that to use the static variables you have to import them, after that their use is like that of a normal variable

    
answered by 27.06.2018 в 22:08