From f379a0720fb63f42e6f995a630c3b6f37ef48283 Mon Sep 17 00:00:00 2001 From: "suplex.saket@gmail.com" Date: Mon, 2 Jan 2017 18:28:39 +0530 Subject: [PATCH 1/2] Removes the callback from the handler when the activity is destroyed --- .../toolkit/activity/HomeActivity.java | 22 +++++++++++++++---- .../app/src/main/res/values/strings.xml | 1 + 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java b/source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java index ba33a2c3..5930a096 100755 --- a/source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java +++ b/source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java @@ -38,6 +38,9 @@ public class HomeActivity extends AppCompatActivity private final String FRAGMENT_TAG_PROJECT = "Project"; private final String FRAGMENT_TAG_APK = "Apk"; private boolean backPressedOnce = false; + private Runnable runnable; + private Handler handler = new Handler(); + private long timer = 2000; private SmoothNavigationToggle smoothNavigationToggle; @@ -211,16 +214,17 @@ public void onBackPressed() { finish(); } if(!backPressedOnce) - Toast.makeText(this, "Tap back once more to exit.", Toast.LENGTH_SHORT).show(); + Toast.makeText(this, R.string.tap_to_exit, Toast.LENGTH_SHORT).show(); backPressedOnce=true; - new Handler().postDelayed(new Runnable() + runnable = new Runnable() { @Override public void run() { - backPressedOnce= false; + backPressedOnce = false; } - }, 2000); + }; + handler.postDelayed(runnable , timer); } else { fragmentManager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .replace(R.id.container, new HomeFragment(), FRAGMENT_TAG_HOME).commit(); @@ -230,6 +234,16 @@ public void run() navigationView.setCheckedItem(R.id.nav_home); } } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (handler != null) + { + handler.removeCallbacks(runnable); + } + } + } diff --git a/source-code/app/src/main/res/values/strings.xml b/source-code/app/src/main/res/values/strings.xml index 0c11afc6..59ad334a 100644 --- a/source-code/app/src/main/res/values/strings.xml +++ b/source-code/app/src/main/res/values/strings.xml @@ -10,6 +10,7 @@ Home Welcome to BuildmLearn Toolkit Welcome + Tap back once more to exit. Item deleted From 4470b1256583f0eec1832c524a6033079c986fe0 Mon Sep 17 00:00:00 2001 From: "suplex.saket@gmail.com" Date: Fri, 20 Jan 2017 01:23:40 +0530 Subject: [PATCH 2/2] Reformated the code --- .../toolkit/activity/HomeActivity.java | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java b/source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java index 5930a096..c1b5cf0c 100755 --- a/source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java +++ b/source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java @@ -1,19 +1,18 @@ package org.buildmlearn.toolkit.activity; import android.app.Fragment; +import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; +import android.os.Handler; import android.preference.PreferenceManager; import android.support.design.widget.NavigationView; - -import android.app.FragmentManager; import android.support.v4.view.GravityCompat; -import android.os.Handler; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar;; +import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; import android.widget.TextView; @@ -27,6 +26,8 @@ import org.buildmlearn.toolkit.fragment.SettingsFragment; import org.buildmlearn.toolkit.utilities.SmoothNavigationToggle; +; + /** * @brief Home screen of the application containg all the menus and settings. */ @@ -37,15 +38,13 @@ public class HomeActivity extends AppCompatActivity private final String FRAGMENT_TAG_HOME = "Home"; private final String FRAGMENT_TAG_PROJECT = "Project"; private final String FRAGMENT_TAG_APK = "Apk"; + NavigationView navigationView; private boolean backPressedOnce = false; private Runnable runnable; private Handler handler = new Handler(); private long timer = 2000; - private SmoothNavigationToggle smoothNavigationToggle; - NavigationView navigationView; - /** * {@inheritDoc} */ @@ -72,7 +71,7 @@ protected void onCreate(Bundle savedInstanceState) { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); smoothNavigationToggle = new SmoothNavigationToggle( - this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){ + this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); @@ -111,7 +110,7 @@ public boolean onNavigationItemSelected(MenuItem item) { getSupportActionBar().setDisplayShowTitleEnabled(true); } - switch(id){ + switch (id) { case R.id.nav_home: smoothNavigationToggle.runWhenIdle(new Runnable() { @Override @@ -130,7 +129,7 @@ public void run() { @Override public void run() { fragmentManager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) - .replace(R.id.container, new LoadProjectFragment(),FRAGMENT_TAG_PROJECT).commit(); + .replace(R.id.container, new LoadProjectFragment(), FRAGMENT_TAG_PROJECT).commit(); if (getSupportActionBar() != null) { getSupportActionBar().setTitle(R.string.menu_load_project); } @@ -143,7 +142,7 @@ public void run() { @Override public void run() { fragmentManager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) - .replace(R.id.container, new LoadApkFragment(),FRAGMENT_TAG_APK).commit(); + .replace(R.id.container, new LoadApkFragment(), FRAGMENT_TAG_APK).commit(); if (getSupportActionBar() != null) { getSupportActionBar().setTitle(R.string.menu_load_apks); } @@ -210,21 +209,19 @@ public void onBackPressed() { if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else if (fragment != null && fragment.isVisible()) { - if(backPressedOnce){ + if (backPressedOnce) { finish(); } - if(!backPressedOnce) + if (!backPressedOnce) Toast.makeText(this, R.string.tap_to_exit, Toast.LENGTH_SHORT).show(); - backPressedOnce=true; - runnable = new Runnable() - { + backPressedOnce = true; + runnable = new Runnable() { @Override - public void run() - { + public void run() { backPressedOnce = false; } }; - handler.postDelayed(runnable , timer); + handler.postDelayed(runnable, timer); } else { fragmentManager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .replace(R.id.container, new HomeFragment(), FRAGMENT_TAG_HOME).commit(); @@ -238,8 +235,7 @@ public void run() @Override protected void onDestroy() { super.onDestroy(); - if (handler != null) - { + if (handler != null) { handler.removeCallbacks(runnable); } }