0

I have a problem when resuming my application. I can create my activity just fine but for some reason when I put my app in the background, wait a few minutes, then have it come back to the foreround DrawerLayout, is always null. The first time when I load my application it seems to work fine but I can't seem to figure out what is going wrong when I re-create the activity from the background. Here is my code:

MainActivity.Java:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //Listen for navigation events
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){

        public void onDrawerOpened(View view) {
            super.onDrawerOpened(view);
            ...
        }
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            ...
        }
    System.out.println("Toolbar: " + toolbar);
    System.out.println("Drawer: " + drawer);
    System.out.println("Toggle: " + toggle);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true" tools:openDrawer="start">

    <include layout="@layout/app_bar_news_feed" android:layout_width="match_parent"
        android:layout_height="match_parent" android:id="@+id/app_bar_main" />

    <include layout="@layout/include_progress_overlay"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                layout="@layout/nav_header_friends_list"
                android:id="@+id/navigation_header"/>

            <ListView
                android:id="@+id/friends_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"></ListView>

        </LinearLayout>

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

The first time the application loads, I can successfully print out an object when I print out "Drawer: ", but when I set it to the background then have it come back to the foreground after a few minutes, it seems to print out null and I have no idea why. Any help would be appreciated. Thanks!

user1871869
  • 3,317
  • 13
  • 56
  • 106
  • You have to learn how to use the Bundle savedState that you have there bro. It will not only do that when you set it in background but it will also do that when you change from Portrait to Landscape. Check the documentation about the saved Instances and recreate them everytime you go back to your Activity. – Aizen Mar 12 '16 at 20:59
  • @Aizen I understand how to use Bundle savedState but I don't really get why my DrawerLayout will turn into null if I call `findViewById` on the `DrawerLayout`. Wouldn't that re-initialize the drawer for me at that point, so I don't need Bundle savedState? – user1871869 Mar 12 '16 at 21:05
  • You're correct on a part of "Wouldn't that re-initialize the drawer for me at that point" But as you can see. It won't be called. If you check the Android Life Cycle. The onCreate will only be called once, the next is onResume. Which will not re-reference your Variables. If you want, you can overide onResume and re-point your variables. But if you have many variables in the long run, it is best to use Bundles my friend. Check the LifeCycle here so you can Understand what I am saying http://stackoverflow.com/questions/8515936/android-activity-life-cycle-what-are-all-these-methods-for – Aizen Mar 12 '16 at 21:11
  • I tried your code. You have to Re-Inflate your xml. and is working now. Good Luck. Just one line of code you need to add `Inflate` – Aizen Mar 12 '16 at 21:20
  • Before you setContentView ... Reinflate. – Aizen Mar 12 '16 at 21:21
  • @Aizen ah I see. so it wasn't an issue with the Bundle? I would have to follow something along the lines of this: http://stackoverflow.com/questions/2335813/how-to-inflate-one-view-with-a-layout before I call `setContentView`? – user1871869 Mar 12 '16 at 21:23

1 Answers1

0

You would have to Re-Inflate your XML layout like this.

Try this so this is 2 lines, replace your setContentView

First get the View Group ViewGroup container = (ViewGroup)view.getParent();

Now we need to Inflate your XML

setContentView(View.inflate(getApplicationContext(),R.layout.yourlayout,container));
Aizen
  • 1,807
  • 2
  • 14
  • 28
  • What am I supposed to set the `view` in `ViewGroup container = (ViewGroup)view.getParent();` to? I tried the following: `view = findViewById(R.id.drawer_layout); ViewGroup container = (ViewGroup)view.getParent(); setContentView(View.inflate(getApplicationContext(),R.layout.activity_main, container));` but that didn't work as I get a null pointer exception on `getParent()` – user1871869 Mar 12 '16 at 21:59
  • oh I am sorry, getParent() can only be called from a Fragment. sub Activity. Try this answers http://stackoverflow.com/questions/24612364/how-to-get-root-viewgroup-from-a-layout-that-was-set-using-setcontentview – Aizen Mar 12 '16 at 22:04