0

你好堆栈溢出,

问题

当我运行我的第一个(主要)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);
    }
}

这是我的第二个活动的图像(在我尝试通过倾斜手机将其设置为横向后关闭的活动):

第二项活动 - 鱼类物种清单

知道为什么会发生这种情况,甚至更好,对于这种情况有什么好的解决方法?请记住,我不希望屏幕保持某个方向。如果用户可以在两者之间切换而不关闭它们,我希望它。感谢您的阅读和您的帮助。

4

2 回答 2

0

在 Mainfest xmll 中为您的第二个活动添加此内容

<activity android:name="com.package.SecondActivity" android:configChanges="keyboard|keyboardHidden|orientation" />
于 2012-12-08T16:01:44.780 回答
0

我尝试重现您的问题,对我来说,第二个活动没有关闭,而是在我倾斜手机时正确旋转。但是,当然,我没有你的布局和图像。所以看起来问题不在您发布的代码中,而是在其他地方。很可能你有空间问题(我在处理图像时也遇到了内存问题)。要检查这一点,请尝试注释掉适配器或加载图像的适配器中的代码。在我的情况下,我不得不调整图像以保持它们的小。此外,适配器的代码可能会有所帮助。希望这可以帮助。

于 2012-12-25T10:34:18.440 回答