3

I am trying to send a bundle from one activity to another. When I load the bundle in the recieving activity all the information seems to be null. Here is some code:

BaseActivity.java

private final DrawerLayout.DrawerListener mDrawerListener = new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                if (mDrawerToggle != null) mDrawerToggle.onDrawerSlide(drawerView, slideOffset);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                if (mDrawerToggle != null) {
                    mDrawerToggle.onDrawerOpened(drawerView);
                }
                if (getSupportActionBar() != null) getSupportActionBar().setTitle(R.string.app_name);
            }

            @Override
            public void onDrawerClosed(View drawerView) {       // 사이드 바에서 선택한 리스트 동작
                if (mDrawerToggle != null) mDrawerToggle.onDrawerClosed(drawerView);
                if (mItemToOpenWhenDrawerCloses >= 0) {
                    Bundle extras = ActivityOptions.makeCustomAnimation(
                            BaseActivity.this, R.anim.fade_in, R.anim.fade_out).toBundle();
                    Bundle mediaExtras = new Bundle();

                    Class activityClass = null;
                    switch (mItemToOpenWhenDrawerCloses) {
                        case R.id.navigation_allmusic:
                            activityClass = MusicPlayerActivity.class;
                            break;
                        case R.id.navigation_album:
                            mediaExtras.putString(SAVED_MEDIA_ID, MEDIA_ID_MUSICS_BY_ALBUM);
                            Log.d("123qwer", "onDrawerClosed, mediaExtras -> " + SAVED_MEDIA_ID + " " + MEDIA_ID_MUSICS_BY_ALBUM);
                            activityClass = MusicPlayerActivity.class;
                            break;
                        case R.id.navigation_playlists:
                            //TODO:add class later
    //                        activityClass = PlaceholderActivity.class;
                            break;
                    }
                    if (activityClass != null) {
                        setBundleInfo(mediaExtras);
                        startActivity(new Intent(BaseActivity.this, activityClass).putExtras(mediaExtras), extras);
                        finish();
                    }
                }
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                if (mDrawerToggle != null) mDrawerToggle.onDrawerStateChanged(newState);
            }
        };

MusicPlayerActivity.java

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initializedToolbar();


        Log.d("123qwer", "Get Bundle Test " + this.getIntent().getExtras().getString(SAVED_MEDIA_ID));
        initializeFromParams(savedInstanceState, getIntent());
    }

I want to send the mediaExtras Bundle in BaseActivity class to another activity when I click some button. You can see this part in BaseActivity.

case R.id.navigation_album:
    mediaExtras.putString(SAVED_MEDIA_ID, MEDIA_ID_MUSICS_BY_ALBUM);
    Log.d("123qwer", "onDrawerClosed, mediaExtras -> " + SAVED_MEDIA_ID + " " + MEDIA_ID_MUSICS_BY_ALBUM);
    activityClass = MusicPlayerActivity.class;
    break;

I tried to put String information to mediaExtras. But, when I tried to get this, I just got a null value.

this.getIntent().getExtras().getString(SAVED_MEDIA_ID)

Please teach me how to solve it

Soonmyun Jang
  • 300
  • 5
  • 12
  • Does this line get printed on the logcat? `Log.d("123qwer", "onDrawerClosed, mediaExtras -> " + SAVED_MEDIA_ID + " " + MEDIA_ID_MUSICS_BY_ALBUM);` – Ishita Sinha Jun 14 '16 at 06:08
  • What do u mean by `...putExtras(mediaExtras), extras)` ? and `mediaExtras` and `extras` both is Bundle objects. use a key as first param in putExtras to add Bundle in Intent then use same key for accessing Bundle in next Activity – ρяσѕρєя K Jun 14 '16 at 06:09
  • http://meta.stackexchange.com/questions/473/can-i-re-ask-a-question-if-it-hasnt-been-answered – Arjan Jun 14 '16 at 06:34

1 Answers1

6

First create the data bundel and attach it to intent

Intent mainIntent = new Intent(getApplicationContext(),XXXX.class);
Bundle dataBundle = new Bundle();
dataBundle.putString("keyid","thevalue");
mainIntent.putExtras(dataBundle);
startActivity(mainIntent);

to get this bundle in the XXXX Activity

//getting the datbundel from other activity incoming
Bundle extras = getIntent().getExtras();
String a=extras.getString("keyid");
Wesley
  • 4,084
  • 6
  • 37
  • 60
VYSHAK M
  • 86
  • 5