I've looked at similar questions regarding a similar issue, but I am yet to find a solution to my problem.
I have an application using navigation drawer. I have a MainActivity, various FragmentActivities & various fragment layouts. My MainActivity code is below:
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
ourSong = MediaPlayer.create(MainActivity.this, R.raw.audirs6);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean music = getPrefs.getBoolean("checkbox", true);
if (music == true)
ourSong.start();
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// AutoMart Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Search For A Car
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
ourSong.release();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
Intent settings = new Intent("com.loui.automartdirectcars.SETTINGS");
startActivity(settings);
break;
case R.id.aboutUs:
Intent about = new Intent("com.loui.automartdirectcars.ABOUT");
startActivity(about);
break;
case (R.id.tellFriend):
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("application/octet-stream");
share.putExtra(Intent.EXTRA_SUBJECT, "AutoMart Direct Cars Android App");
share.putExtra(Intent.EXTRA_TEXT, "Hey check out AutoMart Direct Cars Android App at https://play.google.com/store/apps/details?id=com.loui.automartdirectcars");
startActivity(share);
break;
case R.id.contactDeveloper:
String body = "Enter your Question, Enquiry or Feedback below:\n\n";
Intent contactDev = new Intent(Intent.ACTION_SEND);
contactDev.setType("application/octet-stream");
contactDev.putExtra(Intent.EXTRA_EMAIL, new String[]{"louigi7386@gmail.com"});
contactDev.putExtra(Intent.EXTRA_SUBJECT, "AutoMart Direct Cars Android App");
contactDev.putExtra(Intent.EXTRA_TEXT, body);
startActivity(contactDev);
break;
case R.id.exit:
finish();
break;
}
return super.onOptionsItemSelected(item);
}
/* *
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
menu.findItem(R.id.aboutUs).setVisible(!drawerOpen);
menu.findItem(R.id.tellFriend).setVisible(!drawerOpen);
menu.findItem(R.id.contactDeveloper).setVisible(!drawerOpen);
menu.findItem(R.id.exit).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* @param <FragmentTransaction>
* */
private <FragmentTransaction> void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new SearchCarFragment();
break;
case 2:
fragment = new LogoFlipperFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
default:
break;
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).addToBackStack(null).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
public void onBackPressed() {
WebView mWebView = (WebView) findViewById(R.id.webView);
if (mWebView.canGoBack())
mWebView.goBack();
return;
}
}
Below is Activity for webview:
public class HomeFragment extends Fragment {
public HomeFragment(){}
@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
WebView mWebView = (WebView) rootView.findViewById(R.id.webView);
//Initializing and loading url in webview
mWebView = (WebView) rootView.findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.automartdirect.co.uk");
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
// Otherwise allow the OS to handle it
else if (url.startsWith("tel:")) {
Intent tel = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(tel);
return true;
}
else if (url.startsWith("mailto:")) {
String body = "Enter your Question, Enquiry or Feedback below:\n\n";
Intent mail = new Intent(Intent.ACTION_SEND);
mail.setType("application/octet-stream");
mail.putExtra(Intent.EXTRA_EMAIL, new String[]{"info@automartdirect.co.uk"});
mail.putExtra(Intent.EXTRA_SUBJECT, "Enquiry From AutoMart Direct Cars Android App");
mail.putExtra(Intent.EXTRA_TEXT, body);
startActivity(mail);
return true;
}
return true;
}
});
return rootView;
}
}
I have a fragment layout with a webview and the relative HomeFragment Activity that inflates this webview layout. I've placed onBackPressed method at the bottom of MainActivity to be able to go back within this webview using back button. Everything works as I would like whilst interacting inside the webview. All links open within the webview & pressing back button takes me back to previous webview history as required. When there is no more history in webview & back button is pressed, nothing happens which again is ideal as I don't want the app to be exited. This then requires the user to tap the app icon to open navigation drawer & choose another item in the menu.
Here is where I'm stuck. When I choose another item (fragment) from navigation drawer, it gets displayed, but when I press back button the app closes with message 'Unfortunately, myapp has stopped. Am I missing some code or do I have to add onBackPressed code for each fragment? More importantly, I'm confused as to where the onBackPressed method should go. Should it only go in MainActivity or should it go in each FragmentActivity? As it stands the only onBackPressed method I have is in MainActivity for webview which works, but may still be wrong practice. What code do I need for other fragments that when back button is pressed, it doesn't close app with the above message I mentioned and where should it go?
Any help is much appreciated!
Here is Logcat:
12-17 22:34:03.957: D/AndroidRuntime(8477): Shutting down VM
12-17 22:34:03.957: W/dalvikvm(8477): threadid=1: thread exiting with uncaught exception (group=0x41288930)
12-17 22:34:03.987: E/AndroidRuntime(8477): FATAL EXCEPTION: main
12-17 22:34:03.987: E/AndroidRuntime(8477): java.lang.NullPointerException
12-17 22:34:03.987: E/AndroidRuntime(8477): at com.loui.automartdirectcars.MainActivity.onBackPressed(MainActivity.java:273)
12-17 22:34:03.987: E/AndroidRuntime(8477): at android.app.Activity.onKeyUp(Activity.java:2145)
12-17 22:34:03.987: E/AndroidRuntime(8477): at android.view.KeyEvent.dispatch(KeyEvent.java:2633)
12-17 22:34:03.987: E/AndroidRuntime(8477): at android.app.Activity.dispatchKeyEvent(Activity.java:2375)
12-17 22:34:03.987: E/AndroidRuntime(8477): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1865)
12-17 22:34:03.987: E/AndroidRuntime(8477): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
12-17 22:34:03.987: E/AndroidRuntime(8477): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
12-17 22:34:03.987: E/AndroidRuntime(8477): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
12-17 22:34:03.987: E/AndroidRuntime(8477): at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 22:34:03.987: E/AndroidRuntime(8477): at android.os.Looper.loop(Looper.java:137)
12-17 22:34:03.987: E/AndroidRuntime(8477): at android.app.ActivityThread.main(ActivityThread.java:5195)
12-17 22:34:03.987: E/AndroidRuntime(8477): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 22:34:03.987: E/AndroidRuntime(8477): at java.lang.reflect.Method.invoke(Method.java:511)
12-17 22:34:03.987: E/AndroidRuntime(8477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
12-17 22:34:03.987: E/AndroidRuntime(8477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
12-17 22:34:03.987: E/AndroidRuntime(8477): at dalvik.system.NativeStart.main(Native Method)