Avinash Jaiswal Avinash Jaiswal - 29 days ago 4
Android Question

How to use clickable edittexts which intents to some other layout but on return not lose any data from previous intent?

I am trying to create a layout such that there are two clickable edittexts and onClick of those Edittexts, layout is redirected to some other layout where the user has to choose something. But my question is how to achieve this as I am losing past intent data. When I am clicking on first and then clicking on second then the data returned by the first intent disappears. How do I get a solution for this issue?

Answer

Ok I give you simple startActivityForResult example, can you try this :

In your first Activity :

//declaring this globally would be good
private final int REQUEST_CODE_OTHER_ACTIVITY=0x1;
Intent i=new Intent(this,SecondActivity.class);
startActivityForResult(i,REQUEST_CODE_OTHER_ACTIVITY);

then add this method to catch returning Intents :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode != RESULT_OK) {
        return;
    }

    switch (requestCode) {

        case REQUEST_CODE_OTHER_ACTIVITY:
            Bundle extras=data.getExtras();
            Toast.makeText(context,data.getString("salutation"),Toast.LENGTH_LONG).show();
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

So in your second Activity use like that to return data :

Intent returnIntent = new Intent();
String salute="Hello | привет | Hallo | Merhaba | Bonjour" ;
Bundle extras = new Bundle();
extras.putString("salutation",salute);
setResult(Activity.RESULT_OK,returnIntent.putExtras(extras));    
finish();

Please refer this link : Using Intents to Create Flows

Also this too : Result

There is another way is using Fragments for dynamic layouts. Please try something make some effort, edit your question than I can help you easily.

Comments