From 4a48f367ba2d99e2dbc87124425f228599c41975 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Fri, 2 Dec 2016 12:45:12 +0200 Subject: [PATCH] Fixes on drawer and back button handling Double back to exit app First back closes drawer Toolbar has a back icon instead of drawer icon and drawer only opens with left to right swipe --- .../thmmy/mthmmy/activities/BaseActivity.java | 12 ++++++++++- .../mthmmy/activities/main/MainActivity.java | 21 ++++++++++++++++--- .../activities/topic/TopicActivity.java | 9 ++++++++ 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java index 44205e3a..b3cc4176 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/BaseActivity.java @@ -8,6 +8,8 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.util.Log; +import android.view.MenuItem; import android.view.View; import android.widget.Toast; @@ -62,7 +64,6 @@ public class BaseActivity extends AppCompatActivity sessionManager = new SessionManager(client, cookieJar, sharedPrefsCookiePersistor, sharedPrefs); init = true; } - } public static OkHttpClient getClient() @@ -127,6 +128,15 @@ public class BaseActivity extends AppCompatActivity }) .build(); drawer.setSelection(-1); + + drawer.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false); + drawer.setOnDrawerNavigationListener(new Drawer.OnDrawerNavigationListener() { + @Override + public boolean onNavigationClickListener(View clickedView) { + onBackPressed(); + return true; + } + }); } protected void updateDrawer() diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index 41747d6a..909cb942 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java @@ -8,6 +8,7 @@ import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.widget.Toolbar; +import android.widget.Toast; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.BaseActivity; @@ -23,6 +24,8 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF //----------------------------------------CLASS VARIABLES----------------------------------------- private static final String TAG = "MainActivity"; + private static final int TIME_INTERVAL = 2000; + private long mBackPressed; @Override protected void onCreate(Bundle savedInstanceState) { @@ -61,9 +64,21 @@ public class MainActivity extends BaseActivity implements RecentFragment.OnListF updateDrawer(); } - - - + @Override + public void onBackPressed() { + if(drawer.isDrawerOpen()){ + drawer.closeDrawer(); + return; + } + else if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) { + super.onBackPressed(); + return; + } else { + Toast.makeText(getBaseContext(), "Press back again to exit!" + , Toast.LENGTH_SHORT).show(); + } + mBackPressed = System.currentTimeMillis(); + } @Override public void onFragmentInteraction(TopicSummary topicSummary) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index eca0dacf..d71dadca 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -182,6 +182,15 @@ public class TopicActivity extends BaseActivity { new TopicTask().execute(extras.getString("TOPIC_URL")); //Attempt data parsing } + @Override + public void onBackPressed() { + if (drawer.isDrawerOpen()) { + drawer.closeDrawer(); + return; + } + super.onBackPressed(); + } + @Override protected void onDestroy() { //When finished cancel whatever request can still be canceled super.onDestroy();