2

I'd like to know why PersistableBundle shows me a blank page.

When onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) ,

it shows me a blank page but onCreate(@Nullable Bundle savedInstanceState) shows me a page with BottomavigationBar.

I know PersistableBundle can save data to somewhere, but how does it related to this phenomenon?

Here is the codes.

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

But this does not

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

Thanks in advance.

Saa
  • 79
  • 9
  • Possible duplicate of [when i click on button to showing next activity it shows only white screen](https://stackoverflow.com/questions/40327208/when-i-click-on-button-to-showing-next-activity-it-shows-only-white-screen) – lidkxx Dec 13 '18 at 13:03
  • No, actually it's different. The codes themselves are the same, but the question is different. Now I know the direct reason for showing a blank page, but don't know the root cause. So I asked this question. – Saa Dec 13 '18 at 14:08
  • The `onCreate()` overload that takes a `PersistableBundle` is not going to be called during normal `Activity` startup. It's used to restore state after a device reboot. When you use that overload, instead of the usual one, that method doesn't get called at all, so your `setContentView(R.layout.activity_home);` call and the subsequent setup never happen. – Mike M. Dec 13 '18 at 18:55
  • Thank you. So I need 2 onCreate() for the one retaining data and the one used when it's called? – Saa Dec 14 '18 at 08:40
  • Not necessarily, unless you're wanting to save runtime state across reboots. If not, then the single-parameter `onCreate()` override is sufficient. – Mike M. Dec 14 '18 at 21:14
  • I get it! Thanks Mike, your answer helped me a lot. By the way it's comment section so I can't select you as best answer. If you don't care I will just write your answer by myself, or you can just copy your answer and I can select you as best. – Saa Dec 14 '18 at 23:12
  • You're more than welcome to post an answer. Glad I could help clear things up. Cheers! – Mike M. Dec 15 '18 at 02:06

1 Answers1

4

Thanks to Mike M, I now understand why.

The onCreate() overload that takes a PersistableBundle is not going to be called during normal Activity startup. It's used to restore state after a device reboot. When you use that overload, instead of the usual one, that method doesn't get called at all, so your setContentView(R.layout.activity_home); call and the subsequent setup never happen.

Saa
  • 79
  • 9