0

I have a project that contains three activities(MainActivity, PlayerActivity and ListActivity). I want to send intent from MainActivity to PlayerActivity and select a name in PlayerActivity and pass it again toMainActivity.

I wrote another intent to send some data to ListActivity fromMainactivity.

MainActivity to ListActivity works perfect, also MainActivityto PlayerActivity. but when PlayerActivitysend Intent to MainActivity, I get a null intent.

Here is my code:

Send From ListActivity:

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = getIntent();
            Bundle bundle = intent.getExtras();

            int buttonId = bundle.getInt("buttonID");
            String name = parent.getItemAtPosition(position).toString();

            Intent sendIntentToMainActivity = new Intent(PlayersActivity.this, MainActivity.class);
            Bundle bundle1 = new Bundle();

            bundle1.putString("name", name);
            bundle1.putInt("buttonId", buttonId);

            startActivity(sendIntentToMainActivity);

        }
    });

Get in MainActivity:

@Override
protected void onNewIntent(Intent intent) {
    if (intent != null) {
        setIntent(intent);
        Log.d("xxx", "Intent is null");
    }
}

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

    Intent intent = getIntent();
    Bundle bundle = new Bundle();

    int id = bundle.getInt("buttonID");
    String name = bundle.getString("name");

    if (id == mButton_first_group_frist_name.getId()) {
        mButton_first_group_frist_name.setText(name);
    } else if (id == mButton_first_group_second_name.getId()) {
        mButton_first_group_second_name.setText(name);
    } else if (id == mButton_second_group_frist_name.getId()) {
        mButton_second_group_frist_name.setText(name);
    } else if (id == mButton_second_group_second_name.getId()) {
        mButton_second_group_second_name.setText(name);
    }
}

Please help and give advice

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

You have to add the bundle to the created intent, and when back to onResume, get the bundle from the intent. You can do that this way:

Intent sendIntentToMainActivity = new Intent(PlayersActivity.this, MainActivity.class);
            Bundle bundle1 = new Bundle();

            bundle1.putString("name", name);
            bundle1.putInt("buttonId", buttonId);


            sendIntentToMainActivity.putExtra("bundle",bundle1);
            startActivity(sendIntentToMainActivity);

And then inside onResume :

Intent intent = getIntent();
    Bundle bundle = intent.getBundleExtra("bundle");
Gratien Asimbahwe
  • 1,606
  • 4
  • 19
  • 30