我正在开发一个Android应用程序。在此应用程序中,我有3个活动1作为标题活动,另一个作为字幕活动,另一个是详细活动
现在我想向上导航从一个活动(字幕活动)到父活动(标题活动 )通过使用以下代码,我执行该操作
SubTitleActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subcategory_activity);
actionBar=getSupportActionBar();
listView2= (ListView) findViewById(R.id.listView2);
Intent intent= getIntent();
final String selectedData = intent.getStringExtra("selectedItem");
actionBar.setTitle(selectedData);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase=dbHelper.getReadableDatabase();
cursor=dbHelper.getsubcategory(sqLiteDatabase,selectedData);
String[] from = new String[] { dbHelper.SUBCATEGORY };
int[] to = new int[] {R.id.subcategory };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.subcategory_row,cursor,from,to);
adapter.notifyDataSetChanged();
listView2.setAdapter(adapter);
listView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = (Cursor) (listView2.getItemAtPosition(position));
String selectedItem = cursor.getString(0);
Intent intent = new Intent(SubcategoryActivity.this, DetailActivity.class);
intent.putExtra("selectedItem", selectedItem);
startActivity(intent);
}
});
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();
if (id==android.R.id.home)
{
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
}
的Manifest.xml
<activity
android:name=".SubcategoryActivity"
android:label="@string/title_activity_subcategory" >
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"></meta-data>
</activity>
现在我不知道如何从我的细节活动转移到子活动,我们怎么能执行呢?
DetailActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_activity);
actionBar=getSupportActionBar();
detailtext= (TextView) findViewById(R.id.detail);
imageView= (ImageView) findViewById(R.id.images);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent= getIntent();
final String selectedData = intent.getStringExtra("selectedItem");
actionBar.setTitle(selectedData);
dbHelper = new SqlLiteDbHelper(this);
try {
dbHelper.openDataBase();
} catch (SQLException e) {
e.printStackTrace();
}
sqLiteDatabase = dbHelper.getReadableDatabase();
cursor=dbHelper.getdetails(sqLiteDatabase, selectedData);
if(cursor.moveToFirst())
{
detailtext.setText(cursor.getString(0));
}
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();
if (id==android.R.id.home)
{
onBackPressed();
}
答案 0 :(得分:3)
在DetailsActivity
代码中执行以下操作:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
有关Tasks and Back Stack
(特别是Using Intent Flags
部分)的更多信息,请访问:http://developer.android.com/guide/components/tasks-and-back-stack.html
答案 1 :(得分:-1)
使用
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(View.OnClickListener);
并点击监听器,如:
new View.OnClickListener() {
@Override
public void onClick(View view) {
YourActivity.this.onBackPressed();
}