这是我在主页上的onbackpressed。如何在我的项目上处理这个挑战。这样在一个背压到主页上双重停止应用程序???我已经更新了java文件 如果用户点击一个从片段到这个家庭的背景,并且双重停止应用程序?
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.widget.Toast;
public class Menu extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
android.app.Fragment fragment = new MenuFragment(); // create a fragement object
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
//this the email icon on the home page
@Override
public void onClick(View view) {
String myEmail[]={"john@balloonventures.com"};
Intent sendMail = new Intent(Intent.ACTION_SEND);
sendMail.putExtra(Intent.EXTRA_EMAIL,myEmail);
sendMail.putExtra(Intent.EXTRA_SUBJECT,"(Type your subject)");
sendMail.setType("plain/text");
//incase you have to add something else put here
sendMail.putExtra(Intent.EXTRA_TEXT,"Your phone:+2547");
startActivity(sendMail);
}
});
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);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
//taxi module
public void taxi_id(View view){
Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.Taxi.class);
startActivity(timer);}
//host home module
public void hosthome_id(View view){
Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.MainActivity.class);
startActivity(timer);}
//calling enterprenuers module
public void enter_id(View view){
Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.SectionListView.class);
startActivity(timer);}
//calling UKV/ICV
public void ukvicv_id(View view){
Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.UkvIcv_Home.class);
startActivity(timer);}
public void finacial_id(View view){
Intent finacial=new Intent(this,sampleapp.razen.com.sampleapp.FinacialHomePage.class);
startActivity(finacial);}
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.Menu.class);
startActivity(timer);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void displayView(int viewId) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (viewId) {
case R.id.nav_12wks_program:
Intent timer=new Intent(this,sampleapp.razen.com.sampleapp.ExpandableListMainActivity.class);
startActivity(timer);
break;
case R.id.nav_emergency_contact:
Intent flow=new Intent(this,sampleapp.razen.com.sampleapp.EmergencyContact.class);
startActivity(flow);
break;
case R.id.nav_survey:
fragment = new SurveyFragment();
title = "Survey";
break;
case R.id.nav_psld:
fragment = new PsldFragment();
title = "Psld";
break;
case R.id.nav_security:
fragment = new SecurityFragment();
title = "All Volunteers Mu";
break;
case R.id.nav_ukv:
fragment = new UkvKiswaFragment();
title = "Ukv's kiswahili";
break;
case R.id.nav_agreement:
fragment = new AgreementFragment();
title = "Agreement ";
break;
case R.id.nav_njoro_geo:
fragment = new GeoNjoroFragment();
title = "Njoro Geography ";
break;
}
if (fragment != null) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment);
ft.commit();
}
// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
} @SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
displayView(item.getItemId());
return true;
}
}
答案 0 :(得分:0)
创建一个计时器机制(50-100毫秒计时器。您可以使用Timer,TimerTask类)来了解后续用户对后退按钮的点击,并根据点击和计时器生命周期设置一些标志值。如果用户在定时器到期之前点击两次,这意味着你必须在定时器到期时退出应用程序,转到主页面。
答案 1 :(得分:0)
更改您的onBackPressed()
,如下所示:
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
您必须将片段事务添加到backstack,以便正确地对片段进行反向导航。
更改您的displayView(...)
,如下所示:
...
..
if (fragment != null) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.mainFrame, fragment)
.addToBackStack(null);//Note this
ft.commit();
}
...
..
同时更改你的onCreate()如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
android.app.Fragment fragment = new MenuFragment(); // create a fragement object
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.mainFrame, fragment);//Note this
ft.commit();
...
..
}
答案 2 :(得分:0)
在onBackPressed
内,如果背斜堆中有任何片段而不是popBackStack()
,则只需finish()
您的活动。
有关详细信息,请查看How to close one fragment when isVisible