Hello Stack Overflow,
当我运行我的第一个(主要)android活动并更改屏幕方向时, 该应用程序正常工作,我可以不断改变方向 它仍然有效。
当我点击第一个活动上的按钮进入第二个活动时, 屏幕发生变化,一切都很好,我上下滚动(一切都很好)
但是,当我现在切换方向时(在第二个活动上),活动将关闭,没有日志错误,并恢复为第一个活动。
所以我的问题是如何保留切换屏幕方向而不关闭第二个活动的能力?不确定是什么导致了这一点。我已经读过,无论何时屏幕方向发生变化,它都必须销毁并重新创建活动。但是如果它正在处理第一个活动,那为什么它不能用于第二个活动?
这是第一个活动代码:
[Activity(Label = "FishinTales: Main Menu", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity_View_MainMenu : Activity
{
#region Components
private Model n_model;
private GridView n_mainMenuGridView;
#endregion
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
/*
Load data using if statements. Load serializeable if no settings file.
Or create new and test if it is accurately passing the custom class to another activity.
*/
if (((MyApp) this.ApplicationContext).FishingData == null)
{
((MyApp) this.ApplicationContext).LoadFishinTales();
this.n_model = ((MyApp) this.ApplicationContext).FishingData;
}
else
{
this.n_model = ((MyApp) this.ApplicationContext).FishingData;
}
// Set our view from the "View_MainMenu" layout resource
SetContentView(Resource.Layout.View_MainMenu);
this.n_mainMenuGridView = FindViewById<GridView> (Resource.Id.xml_mainMenuGridView);
this.n_mainMenuGridView.SetNumColumns(2);
this.n_mainMenuGridView.Adapter = new MainMenuGridAdapter (this);
this.n_mainMenuGridView.ItemClick += (o, e) => {
if (e.Position == 0)
{
// Navigate to Fish Species
Intent intent = new Intent(this, typeof(Activity_View_FishSpecies));
this.StartActivityForResult(intent, RequestCodes.View_FishSpecies);
}
else if (e.Position == 1)
{
// Navigate to My Favorite Spots
Toast.MakeText(this, "TODO: Navigate to My Favorite Sports", ToastLength.Long).Show();
//Intent intent = new Intent(this, typeof(View_MyFavoriteSpots));
//this.StartActivityForResult(intent, RequestCodes.View_MyFavoriteSpots);
}
else if (e.Position == 2)
{
// Navigate to My Season
Toast.MakeText(this, "TODO: Navigate to My Season", ToastLength.Long).Show();
//Intent intent = new Intent(this, typeof(View_MySeason));
//this.StartActivityForResult(intent, RequestCodes.View_MySeason);
}
else if (e.Position == 3)
{
// Navigate to Inventory
Toast.MakeText(this, "TODO: Navigate to Inventory", ToastLength.Long).Show();
//Intent intent = new Intent(this, typeof(View_Inventory));
//this.StartActivityForResult(intent, RequestCodes.View_Inventory);
}
else if (e.Position == 4)
{
// Navigate to Fishing News
Toast.MakeText(this, "TODO: Navigate to Fishing News", ToastLength.Long).Show();
//Intent intent = new Intent(this, typeof(View_FishingNews));
//this.StartActivityForResult(intent, RequestCodes.View_FishingNews);
}
else if (e.Position == 5)
{
// Navigate to Settings
Toast.MakeText(this, "TODO: Navigate to Settings", ToastLength.Long).Show();
//Intent intent = new Intent(this, typeof(View_Settings));
//this.StartActivityForResult(intent, RequestCodes.View_Settings);
}
else
{
// Invalid Response
Toast.MakeText(this, "Invalid Menu Selection", ToastLength.Long).Show();
}
};
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
// Possibly save data after activity result.?.
}
}
这是我的第一个活动的图像(改变方向的活动没有问题):
这是第二个活动代码:
[Activity(Label = "FishinTales: Fish Species")]
public class Activity_View_FishSpecies : Activity
{
#region Components
private Model n_model;
private ListView n_fishSpeciesListView;
#endregion
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Get Application Global Model
this.n_model = ((MyApp) this.ApplicationContext).FishingData;
// Set our view from the "View_FishSpecies" layout resource
SetContentView(Resource.Layout.View_FishSpecies);
this.n_fishSpeciesListView = FindViewById<ListView> (Resource.Id.xml_fishSpeciesListView);
this.n_fishSpeciesListView.Adapter = new FishSpeciesListAdapter (this.ApplicationContext, this.n_model.SpecieManager.Species);
}
}
这是我的第二个活动的图像(在我尝试通过倾斜手机将其设置为横向方向后关闭的活动):
知道为什么会发生这种情况甚至更好,对于这种情况会有什么好处?请记住,我不希望屏幕保持一定的方向。我希望如果用户可以在两者之间切换,而不关闭它们。感谢您的阅读和帮助。
答案 0 :(得分:0)
在Manifest xml中添加第二个活动
<activity android:name="com.package.SecondActivity" android:configChanges="keyboard|keyboardHidden|orientation" />
答案 1 :(得分:0)
我尝试重现你的问题,对我来说,第二项活动没有关闭,但在我倾斜手机时正确旋转。但是,当然,我没有你的布局和图像。所以看起来问题不在你发布的代码中,而是在其他地方。 很可能你有空间问题(我在使用图像时也遇到了内存问题)。要检查这一点,请尝试注释掉适配器或加载图像的适配器中的代码。 在我的情况下,我不得不重新调整图像以保持它们的小。 此外,适配器的代码可能会有所帮助。 希望这会有所帮助。