Skip to content

Commit 3588385

Browse files
author
free4murad
committed
Double back press implemented.
1 parent 6c603ba commit 3588385

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

source-code/app/src/main/java/org/buildmlearn/toolkit/activity/HomeActivity.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import android.app.FragmentTransaction;
66
import android.content.Intent;
77
import android.os.Bundle;
8+
import android.os.Handler;
89
import android.support.v4.widget.DrawerLayout;
910
import android.support.v7.app.AppCompatActivity;
1011
import android.support.v7.widget.Toolbar;
12+
import android.widget.Toast;
1113

1214
import org.buildmlearn.toolkit.R;
1315
import org.buildmlearn.toolkit.constant.Constants;
@@ -25,6 +27,8 @@ public class HomeActivity extends AppCompatActivity
2527

2628
private Section currentSection;
2729
private NavigationDrawerFragment mNavigationDrawerFragment;
30+
private boolean atHome = true;
31+
private boolean backPressedOnce = false;
2832

2933
/**
3034
* {@inheritDoc}
@@ -130,9 +134,24 @@ public void onBackPressed() {
130134
mNavigationDrawerFragment.closeDrawer();
131135
return;
132136
}
133-
if (getFragmentManager().getBackStackEntryCount() <= 1) {
134-
finish();
137+
if(atHome){
138+
if(backPressedOnce){
139+
finish();
140+
}
141+
backPressedOnce=true;
142+
Toast.makeText(this, "Tap back once more to exit.", Toast.LENGTH_SHORT).show();
143+
new Handler().postDelayed(new Runnable()
144+
{
145+
@Override
146+
public void run()
147+
{
148+
backPressedOnce= false;
149+
}
150+
}, 2000);
151+
}
152+
else if(!atHome){
153+
onNavigationDrawerItemSelected(0);
154+
atHome = true;
135155
}
136-
super.onBackPressed();
137156
}
138157
}

0 commit comments

Comments
 (0)