在onCreate中使用PersistableBundle时,它显示空白页面

时间:2018-12-13 10:36:41

标签: android android-studio

我想知道为什么PersistableBundle给我显示空白页。

何时 onCreate(@Nullable Bundle savedInstanceState,@Nullable PersistableBundlepersistentState),

它显示了一个空白页 但是onCreate(@Nullable Bundle savedInstanceState)  显示给我带有BottomavigationBar的页面。

我知道PersistableBundle可以将数据保存到某个地方,但是与这种现象有什么关系?

这是密码。

package com.example.android.instagramclone.Home;

import android.content.Context;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.example.android.instagramclone.R;
import com.example.android.instagramclone.Utils.BottomNavigationViewHelper;

public class HomeActivity extends AppCompatActivity {

    //What activity it's going through
    private static final String TAG = "HomeActivity";
    private static final int ACTIVITY_NUM = 0;



    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

        Context mContext = HomeActivity.this;
        setContentView(R.layout.activity_home);
        Log.d(TAG, "onCreate: starting.");

        setupBottomNavigationView(mContext);
    }

    /**
     * BottomNavigationViewSetup
     */
    private void setupBottomNavigationView(Context mContext){
        Log.d(TAG, "setBottomNavigationView: setting up bottomNavigationView");
        BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomNavViewBar);
        BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationView);
        Menu menu = bottomNavigationView.getMenu();
        MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
        menuItem.setChecked(true);
    }
}

但这不是

package com.example.android.instagramclone.Likes;

import android.content.Context;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.example.android.instagramclone.R;
import com.example.android.instagramclone.Utils.BottomNavigationViewHelper;

public class LikesActivity extends AppCompatActivity {

    private static final String TAG = "LikesActivity";
private Context mContext = LikesActivity.this;
    private static final int ACTIVITY_NUM = 3;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_home);
        Log.d(TAG, "onCreate: started.");

        setupBottomNavigationView();
    }

    //set up bottomNavigationView
    private void setupBottomNavigationView(){
        Log.d(TAG, "setBottomNavigationView: setting up bottomNavigationView");
        BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomNavViewBar);
 //       BottomNavigationViewHelper.setUpBottomNavigationView(bottomNavigationView);
        BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationView);
        Menu menu = bottomNavigationView.getMenu();
        MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
        menuItem.setChecked(true);
    }
}

谢谢。

1 个答案:

答案 0 :(得分:0)

感谢Mike M,现在我明白了为什么。

在正常的Activity启动期间不会调用需要PersistableBundle的onCreate()重载。重新启动设备后,它可用于恢复状态。当您使用该重载而不是通常的重载时,根本不会调用该方法,因此您的setContentView(R.layout.activity_home);呼叫,随后的设置永远不会发生。