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!