0

I have two activities: MainActivity and SecondActicity. On MainActivity I click on a button and go to SecondActicity:

public void GoToSeconActivity(View view) {
    Intent intent = new Intent(this, secondActivity.class);;
    startActivity(intent);
}

On secondActivity I click on a button and then I go back to MainActivity and also send extras:

public void goBackToMainActivity() {
    Intent intent = new Intent(getBaseContext(), MainActivity.class);
    intent.putExtra("myVar", myVar);
    startActivity(intent);
}

on MainActivity's onCreate I want to show myVar in an EditText

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
        .add(R.id.container, new PlaceholderFragment())
        .commit();
    }

    Bundle extras = getIntent().getExtras();
    if (extras != null) 
    {
        View myView = getLayoutInflater().inflate(R.layout.fragment_main, null);
        EditText myVarEdittext1 =   (EditText)myView.findViewById(R.id.myVarEditText);
        string str = extras.getString("myVar");
        myVarEdittext1.setText(str, TextView.BufferType.EDITABLE);
    }
}

But it doesn't work. I try to add myVarEdittext1.postInvalidate(); after setText and it didn't help. I also tried to put this code in

 mHandler.post(new Runnable() {
     @Override
    public void run() {
        View myView = ....
        //...
        myVarEdittext1.setText(str, TextView.BufferType.EDITABLE);
    }
 });

and it didn't help too...

What else can I do? There is no exception - but the editText remains empty.

gurfi
  • 3
  • 2
  • Before setting `myVar` into `EditText` please `Toast` the value of `myVar` to check if there is any value for `myVar` – Lal Jun 29 '14 at 16:11
  • What is Toast? Anyway I also tried to put "blabla" in setText(), without being depended on extras, and it didn't work. – gurfi Jun 29 '14 at 16:14
  • Put this `Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();` instead of `myVarEdittext1.setText(str, TextView.BufferType.EDITABLE);` and check if anything is displayed.. – Lal Jun 29 '14 at 16:24
  • @codeMagic - still not working, editText stays empty... – gurfi Jun 29 '14 at 16:28
  • Did you try adding the toast????This is just for debugging..Please do it.. – Lal Jun 29 '14 at 16:32

3 Answers3

0

You need to override onActivityResult in MainActivity and extract myVar from there:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    string str = data.getString("myVar");
}

And you also need to change call from startActivity to startActivityForResult:

Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, 1);
Green goblin
  • 9,898
  • 13
  • 71
  • 100
0

Add the code that gets the Intent data and sets the text on your EditText.

Bundle extras = getIntent().getExtras();
    if (extras != null) 
    {
        View myView = getLayoutInflater().inflate(R.layout.fragment_main, null);
        EditText myVarEdittext1 =   (EditText)myView.findViewById(R.id.myVarEditText);
        string str = extras.getString("myVar");
        myVarEdittext1.setText(str, TextView.BufferType.EDITABLE);
    }

You don't seem to be finishing the Activity so onCreate() is not called but onResume() will be called.

You could also use startActivityForResult().

Here is an example of that

Activity Lifecycle

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

If your Second Activity returns data to your MainActivity then You should have launched the the SecondActivity in the MainActivity Using "startActivityForResult". Then when u launch the SecondActivity in the app , u have to get back to the MainActivity in a different way. Data is returned to the MainActivity using a Uri. That's of Advanced Android Development as a beginner u should not get after these things. If u want to know more about Sub-Activity which is what i gave u , Just perform some google search and examples of subactivities and returning data from them.