onBackPressed() 方法在 Android Navigartion Drawer 中无法正常工作
onBackPressed() method doesn't work as expected in Android Navigartion Drawer
我正在尝试实现 android 导航抽屉。问题出在我的 onBackPressed() 方法上。当按下后退按钮时,它会重定向到我的 MainActivity。但我想将它用于从当前页面转到上一页。
我尝试了很多但做不到。我的 logCat.
没有错误
所以我的 onBackPressed() 是这样的
@Override
public void onBackPressed() {
Intent i=new Intent(MainActivity.this,MainActivity.class);
startActivity(i);
return;
}
所以现在如何解决我的问题。
对于您的理解,有我的 MainActivity 代码
package spurs.cafesoft.travelersguide;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
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.Menu;
import android.view.MenuItem;
import spurs.cafesoft.travelersguide.fragment.Fragment_barishal;
import spurs.cafesoft.travelersguide.fragment.Fragment_chittagong;
import spurs.cafesoft.travelersguide.fragment.Fragment_dhaka;
import spurs.cafesoft.travelersguide.fragment.Fragment_home;
import spurs.cafesoft.travelersguide.fragment.Fragment_khulna;
import spurs.cafesoft.travelersguide.fragment.Fragment_main;
import spurs.cafesoft.travelersguide.fragment.Fragment_mymensingh;
import spurs.cafesoft.travelersguide.fragment.Fragment_rajshahi;
import spurs.cafesoft.travelersguide.fragment.Fragment_rangpur;
import spurs.cafesoft.travelersguide.fragment.Fragment_sylhet;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "ns.nayem350@gmail.com", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_main()).commit();
}
@Override
public void onBackPressed() {
Intent i=new Intent(MainActivity.this,MainActivity.class);
startActivity(i);
return;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
FragmentManager fm =getFragmentManager();
int id = item.getItemId();
if (id==R.id.home){
fm.beginTransaction().replace(R.id.frame_layout,new Fragment_home()).commit();
}
else if (id == R.id.dhaka) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_dhaka()).commit();
} else if (id == R.id.chittagong) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_chittagong()).commit();
} else if (id == R.id.barishal) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_barishal()).commit();
} else if (id == R.id.rajshahi) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_rajshahi()).commit();
} else if (id == R.id.khulna) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_khulna()).commit();
} else if (id == R.id.sylhet) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_sylhet()).commit();
}else if (id == R.id.rangpur) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_rangpur()).commit();
}else if (id == R.id.mymensingh) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_mymensingh()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
您必须在替换片段时将事务添加到 backstack。这样你就不需要自定义 onBackPressed
代码,它应该可以正常工作。
backstack 是 android 系统用来导航回以前的系统状态的堆栈,这就是当人们按下后退按钮时您想要的。
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(..............);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
使用您的代码的示例
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_mymensingh()).addToBackStack(null).commit();
我正在尝试实现 android 导航抽屉。问题出在我的 onBackPressed() 方法上。当按下后退按钮时,它会重定向到我的 MainActivity。但我想将它用于从当前页面转到上一页。 我尝试了很多但做不到。我的 logCat.
没有错误所以我的 onBackPressed() 是这样的
@Override
public void onBackPressed() {
Intent i=new Intent(MainActivity.this,MainActivity.class);
startActivity(i);
return;
}
所以现在如何解决我的问题。 对于您的理解,有我的 MainActivity 代码
package spurs.cafesoft.travelersguide;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
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.Menu;
import android.view.MenuItem;
import spurs.cafesoft.travelersguide.fragment.Fragment_barishal;
import spurs.cafesoft.travelersguide.fragment.Fragment_chittagong;
import spurs.cafesoft.travelersguide.fragment.Fragment_dhaka;
import spurs.cafesoft.travelersguide.fragment.Fragment_home;
import spurs.cafesoft.travelersguide.fragment.Fragment_khulna;
import spurs.cafesoft.travelersguide.fragment.Fragment_main;
import spurs.cafesoft.travelersguide.fragment.Fragment_mymensingh;
import spurs.cafesoft.travelersguide.fragment.Fragment_rajshahi;
import spurs.cafesoft.travelersguide.fragment.Fragment_rangpur;
import spurs.cafesoft.travelersguide.fragment.Fragment_sylhet;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "ns.nayem350@gmail.com", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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);
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_main()).commit();
}
@Override
public void onBackPressed() {
Intent i=new Intent(MainActivity.this,MainActivity.class);
startActivity(i);
return;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
FragmentManager fm =getFragmentManager();
int id = item.getItemId();
if (id==R.id.home){
fm.beginTransaction().replace(R.id.frame_layout,new Fragment_home()).commit();
}
else if (id == R.id.dhaka) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_dhaka()).commit();
} else if (id == R.id.chittagong) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_chittagong()).commit();
} else if (id == R.id.barishal) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_barishal()).commit();
} else if (id == R.id.rajshahi) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_rajshahi()).commit();
} else if (id == R.id.khulna) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_khulna()).commit();
} else if (id == R.id.sylhet) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_sylhet()).commit();
}else if (id == R.id.rangpur) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_rangpur()).commit();
}else if (id == R.id.mymensingh) {
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_mymensingh()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
您必须在替换片段时将事务添加到 backstack。这样你就不需要自定义 onBackPressed
代码,它应该可以正常工作。
backstack 是 android 系统用来导航回以前的系统状态的堆栈,这就是当人们按下后退按钮时您想要的。
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(..............);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
使用您的代码的示例
fm.beginTransaction().replace(R.id.frame_layout, new Fragment_mymensingh()).addToBackStack(null).commit();