本文介绍了如何处理后退按钮转到特定片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用带有片段的导航抽屉制作项目,导航有3个片段.我有问题,当我在第三个片段时,我按下后退按钮时,应用突然关闭,但是我想做的是将片段从第三个片段更改为第一个片段.我该怎么做?
I making project with navigation drawer with fragment, the navigation have 3 fragment. I have problem, when i am at third fragment and i pressed back button the app suddenly close, but the one i want to do is the fragment change from third fragment to first fragment. How i can do that ?
这是我的MainActivity代码:
This is my MainActivity code :
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Toolbar toolbar;
DrawerLayout drawer;
NavigationView navigationView;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Fragment fragment = null;
SessionManager session;
TextView kode_kt, nama_ketua;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
session = new SessionManager(getApplicationContext());
session.checkLogin();
fragmentManager = getFragmentManager();
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.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header=navigationView.getHeaderView(0);
kode_kt = (TextView)header.findViewById(R.id.username);
nama_ketua = (TextView)header.findViewById(R.id.email);
HashMap<String, String> user = session.getUserDetails();
kode_kt.setText(user.get(SessionManager.KEY_KODE));
nama_ketua.setText(user.get(SessionManager.KEY_KETUA));
// email.setText("Jopa Software House");
// tampilan default awal ketika aplikasii dijalankan
if (savedInstanceState == null) {
fragment = new Monitoring();
callFragment(fragment);
}
}
@Override
public void onBackPressed() {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@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;
}
@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) {
Toast.makeText(getApplicationContext(), "Action Settings", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
// Untuk memanggil layout dari menu yang dipilih
if (id == R.id.nav_camera) {
fragment = new Monitoring();
callFragment(fragment);
} else if (id == R.id.nav_gallery) {
showDialog();
} else if (id == R.id.nav_send) {
fragment = new Akun();
callFragment(fragment);
}
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
// untuk mengganti isi kontainer menu yang dipiih
private void callFragment(Fragment fragment) {
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment);
fragmentTransaction.replace(R.id.frame_container, fragment);
fragmentTransaction.commit();
}
private void showDialog(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
this);
// set title dialog
alertDialogBuilder.setTitle("Keluar Akun");
// set pesan dari dialog
alertDialogBuilder
.setMessage("Anda ingin keluar dari akun?")
.setIcon(R.mipmap.ic_launcher)
.setCancelable(false)
.setPositiveButton("Ya",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// jika tombol diklik, maka akan menutup activity ini
session.logoutUser();
}
})
.setNegativeButton("Tidak",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// jika tombol ini diklik, akan menutup dialog
// dan tidak terjadi apa2
dialog.cancel();
}
});
// membuat alert dialog dari builder
AlertDialog alertDialog = alertDialogBuilder.create();
// menampilkan alert dialog
alertDialog.show();
}
}
推荐答案
在活动"中使用此代码
@Override
public void onBackPressed() {
Fragment f = getSupportFragmentManager().findFragmentById(R.id.maincontainer);
if (f instanceof FirstFragment) {
// do operations
} else if (f instanceof SecondFragment) {
// do operations
} else if (f instanceof ThirdFragment) {
// do operations
}else {
super.onBackPressed();
}
}
并使用....
getSupportFragmentManager().popBackStack();
用于从堆栈中删除碎片
这篇关于如何处理后退按钮转到特定片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!