如何将Bundle发送到另一个活动

如何将Bundle发送到另一个活动

本文介绍了Android_如何将Bundle发送到另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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捆绑包发送到另一个活动.您可以在BaseActivity中看到此部分.

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;

我试图将String信息放入mediaExtras.但是,当我尝试获取此值时,我只是得到了一个空值.

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)

请教我如何解决

推荐答案

只需将您的捆绑包放入您用来启动活动的意图中即可.

Just put your bundle in the intent you are using to launch the activity.

Intent mIntent = new Intent(this, ExampleActivitly.class);
Bundle bundle = new Bundle();
bundle.putString("key", value);
mIntent .putExtras(bundle);

在ExampleActivitly中只需

In ExampleActivitlyjust do

Bundle bundle = getIntent().getExtras();
String key = bundle.getString("key")

希望它对您有用:)

这篇关于Android_如何将Bundle发送到另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 02:31