我有一个MainActivity,它有3个片段。其中2个片段中包含列表,单击列表项我转到另一个活动(即NewActivity)。我在NewActivity上实现了一个ActionBar,并希望返回此活动的调用片段。
我有以下代码:
MainActivity.cs
var mongodb = require('mongodb');
function ConnectToDB(mongoUrl){
var MongoClient = mongodb.MongoClient;
//var url = 'mongodb://localhost:27017/my_database_name';
var url = mongoUrl || 'mongodb://' + process.env.IP + ":27017/test";
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
if(err){
console.log(err);
}
console.log("Connected correctly to server");
return db;
});
}
FragmentList1.cs
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
var ft = SupportFragmentManager.BeginTransaction();
ft.Add(Resource.Id.fragment_container, homeFragment, "FRAG_HOME").SetBreadCrumbShortTitle("FRAG_HOME");
ft.Add(Resource.Id.fragment_container, FragmentList1, "FRAG_LIST1").SetBreadCrumbShortTitle("FRAG_LIST1");
ft.Hide(FragmentList1);
ft.Add(Resource.Id.fragment_container, FragmentList2, "FRAG_LIST2").SetBreadCrumbShortTitle("FRAG_LIST2");
ft.AddToBackStack(null);
ft.Hide(FragmentList2);
ft.Commit();
NewActivity.cs
void LvLatestArticles_ItemClick (object sender, AdapterView.ItemClickEventArgs e)
{
Intent intent = new Intent(Activity, typeof(NewActivity));
StartActivity(intent);
}
在上面的代码中,我覆盖了ActionBar上的Home按钮功能。但它根本不做任何事情。我该怎么做呢?任何帮助将不胜感激
答案 0 :(得分:1)
尝试在主页按钮上调用OnBackPressed()
。像这样: -
public override bool OnOptionsItemSelected(IMenuItem item){
switch(item.ItemId){
case Android.Resource.Id.Home:
base.OnBackPressed();
break;
}
return base.OnOptionsItemSelected(item);
}