自从昨天上午以来,我一直在为此而苦苦挣扎。我熟悉C++(只是说我有编程经验)。我正在学习android编程。当我的应用程序启动时,它会启动一个活动。我怀疑它是从导航抽屉回到主页(activity_main)
setContentView(R.layout.activity_main);
在我的抽屉里,我有3个项目。一个被称为“猪崽子”,这是假设是导航家。 “图形”和“设置”是其他2.我想要它,所以当有人点击导航抽屉中的“存钱罐”时,它会返回到程序启动时加载的原始屏幕。这需要在不创建新活动的情况下完成,主屏幕中的数据非常重要,在返回时不应清除。它应该包含留在那里的数据。
这里是我试过
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
android.app.FragmentManager fragmentManager = getFragmentManager();
if (id == R.id.nav_PiggyBank) {
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// getActionBar().setDisplayHomeAsUpEnabled(true);
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);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
} else if (id == R.id.nav_Graph) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new Graph())
.commit();
} else if (id == R.id.nav_Settings) {
fragmentManager.beginTransaction()
.replace(R.id.content_frame
, new UserSettings())
.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
这里是我的activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
什么在你的activity_main? –