我在Activity中使用导航菜单。当我点击菜单时,它会在我给addtobackstack(null)
的时候转到特定的片段。我想解决这两个问题:
do you want to exit app
这是我的家庭活动:
package school.wyse.app.activities;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
public class ParentHomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ServerUtils servutil;
GridLayoutManager grid;
Toolbar toolbar;
private View navHeader;
LinearLayout profile;
ImageView msglist;
ArrayList<StudentProfile> lang_list_new;
public RecyclerView studentsListRecyclerView;
public ParentMenuDashoard mStudAdapter;
StudentProfile[] profiles;
private StudentProfile[] studentCachedData;
Context ctx;
DrawerLayout drawer;
ImageView imageViewstaff;
String tenent_id,profilename,image,userphone;
Integer id,roleid;
String backStateName;
TextView profile_phone,profile_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_parent_home);
backStateName = this.getClass().getName();
ctx=this;
Toast.makeText(ctx, "backStateName--"+backStateName, Toast.LENGTH_SHORT).show();
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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);
navHeader=navigationView.getHeaderView(0);
studentsListRecyclerView = (RecyclerView) navHeader.findViewById(R.id.messageWindowRecycler);
ParentDashboard parentDashboard = new ParentDashboard();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_frame, parentDashboard, parentDashboard.getTag()).commit();
toolbar.setTitle("Dashboard");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
try {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, 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_msg) {
return true;
}*/
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_dashboard) {
ParentDashboard parentDashboard = new ParentDashboard();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_frame, parentDashboard, parentDashboard.getTag()).addToBackStack(null).commit();
//toolbar.setTitle("Dashboard");
} else if (id == R.id.nav_teachers) {
TeacherList teacherList = new TeacherList();
FragmentManager manager = getSupportFragmentManager();
// manager.beginTransaction().replace(R.id.id_content_frame, accountsMenuListFragment, "AccountsMenu").addToBackStack(" ").commit();
manager.beginTransaction().replace(R.id.content_frame, teacherList, teacherList.getTag()).addToBackStack(null).commit();
// toolbar.setTitle("Teachers");
} else if (id == R.id.nav_attendance) {
AttendanceFragment attendanceFragment = new AttendanceFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_frame, attendanceFragment, attendanceFragment.getTag()).addToBackStack(null).commit();
// toolbar.setTitle("Attendance");
} else if (id == R.id.nav_consumablity) {
ParentConsumablityFragment parentConsumablityFragment = new ParentConsumablityFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.content_frame, parentConsumablityFragment, parentConsumablityFragment.getTag()).addToBackStack(null).commit();
// toolbar.setTitle("Attendance");
}
else {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
好的,抱歉迟到的回复
这一部分进入你活动的onBackPressed()
,它基本上会检查你是否附加了片段(所以它不会执行本机返回,这会关闭应用程序)
if (childFragments.size() > 0) {
if (childFragments.get((childFragments.size() - 1)) != null) {
childFragments.remove(childFragments.size() - 1);
return;
}
}
每次add/remove
碎片时,您所要做的就是attach / detach
个项目。
对于您处于片段C并且想要返回片段A而不显示片段B的特定情况,我建议您创建一个公共函数,从ArrayList
除去最后一个片段(从0位置)删除所有片段)
答案 1 :(得分:0)
在主要活动中使用以下代码,以显示上传:
@Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
finish();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce = false;
}
}, 2000);
}
将片段A移动到B时,不要添加&#34; addbackstack&#34;。将片段B移动到C时添加&#34; addbackstack(&#34; C&#34;)&#34;
答案 2 :(得分:0)
首先,你必须处理你的第二个问题,因为它将导致第一个问题的轻松处理,因此你必须注意如何启动片段,以便始终到达第一个片段无论导致当前片段的片段流量如何,这样做,只需启动片段,如下所示:
/**
* To initiate any fragment
*/
private void showAnyFragment(Fragment fragmentToShow) {
getSupportFragmentManager().popBackStack();
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.content);
frameLayout.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction()
.replace(R.id.content, fragmentToShow)
.addToBackStack(null)
.commit();
}
/**
* When (for a reason) you want to initiate the main fragment (FragmentA)
*/
private void homeClicked() {
getSupportFragmentManager().popBackStack();
}
现在,fragmentA
是您的主要片段,从A
到B
到C
再到D
并点击后退按钮它将返回到直接A
。
来到你的第二个问题,它将是直接的,因为你只保留fragmentA
总是在后面的堆栈中,所以除非没有其他片段,否则不会调用Activity的onBackPressed()
函数正在显示FragmentA
以外的其他内容(例如,如果您在fragmentC
并且单击后退按钮,则不会调用onBackPressed()
函数),因此您确定当您进入onBackPressed()
时{ {1}}当前运行的片段为FragmentA
的活动的功能,这是您的主要页面片段。
所以如果你想在用户试图退出应用程序时显示一个弹出窗口(他会在FragmentA
并且回来),你所要做的就是覆盖onBackPressed()
方法保存片段的MainActivity
:
@Override
public void onBackPressed() {
// the user here is in FragmentA(main page),
and he is clicking back to exit the app.
// TODO show popup here or do whatever you want
}