即时通讯编程新,我有一个带碎片的导航抽屉。当我旋转我的设备时,它会“重新启动应用程序”或调用Home片段。我不知道如何解决这个问题,它与super.onCreate(savedInstance)有关,但没有找到如何实现它。我也试着用我的清单上的onConfigurationChanged方法,但没有运气。我一直在复制来自不同教程的代码,以使我的应用程序工作,所以即时通讯不知道如果发布的代码应该是我应该发布的代码。谢谢。方向重新启动我的应用程序
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
ImageView home;
Fragment fragment = null;
TextView appname;
ExpandableListView expListView;
HashMap<String, List<String>> listDataChild;
ExpandableListAdapter listAdapter;
List<String> listDataHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String fontPath = "neue.ttf";
setContentView(R.layout.activity_main);
home = (ImageView)findViewById(R.id.home);
home.setOnClickListener(homeOnclickListener);
appname = (TextView)findViewById(R.id.appname);
Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
appname.setTypeface(tf);
setUpDrawer();
}
private void setUpDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
mDrawerLayout.setDrawerListener(mDrawerListener);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
expListView.setAdapter(listAdapter);
fragment = new Home();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerLayout.closeDrawer(expListView);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
switch (groupPosition) {
case 0:
switch (childPosition) {
case 0:
fragment = new 1();
break;
case 1:
fragment = new 2();
break;
case 2:
fragment = new 3();
break;
...
}
break;
case 1:
switch (childPosition) {
case 0:
fragment = new 4();
break;
...
default:
break;
}
break;
...
}
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerLayout.closeDrawer(expListView);
return false;
}
});
}...
在您的清单文件中添加'机器人:configChanges =“方向”'你的活动标签。这可以防止活动在方向更改中销毁并重新创建。另外,让你的Activity从支持库中扩展AppCompatActivity;这是向后兼容性的更好方法,并确保configeChanges标签能够正常工作。 – chRyNaN
谢谢我尝试它,但它没有工作 –
由于Shadab Ansari回答说,答案帮助我很多,但我不得不去解决其他问题,最后我发现了一个解决方案,以另一种方式没有崩溃我的应用程序的旋转问题,如果你想看到它在这里http://stackoverflow.com/questions/36900671/app-crashing-after-i-rotate-device-and-click-the-menu/36901929#36901929 –