Android如何将Bundle发送到另一个Activity?

时间:2016-06-14 06:03:00

标签: java android android-activity bundle

我正在尝试将捆绑从一个活动发送到另一个活动。当我在接收活动中加载包时,所有信息似乎都为空。这是一些代码:

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());
    }

我想在单击某个按钮时将BaseActivity类中的mediaExtras Bundle发送到另一个活动。您可以在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;

我尝试将String信息放到mediaExtras中。但是,当我试图得到它时,我只得到一个空值。

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

请教我如何解决它

1 个答案:

答案 0 :(得分:1)

首先创建数据bundel并将其附加到intent

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

在XXXX活动中获取此捆绑包

//getting the datbundel from other activity incoming
Bundle extras = getIntent().getExtras();
String a=extras.getString("keyid");