English 中文(简体)
检查汽车在停工时轮值
原标题:Screen auto rotates when unlocking
  • 时间:2011-10-10 22:47:34
  •  标签:
  • android

然而,我试图在我的活动中挽救一些价值观,但我感到有问题,因为我与独居的国家一道拯救了这些价值观,并在居住国恢复,我拯救了几年,第一次拯救了屏幕改变方向,使pin子重新流行,使其达到最后价值,但并不是在我再次改变方向的时候。

我的取向在于我的发言中,我有两种挽救和恢复方法,但似乎不奏效,我是否需要在Pause做些什么?

我的守则是:

    int years;//global variable
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Spinner spinnerYears = (Spinner) findViewById(R.id.spinYears);//Spinner
            final ArrayAdapter <Integer> adapter = new ArrayAdapter <Integer>(this,android.R.layout.simple_spinner_item );
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        for(int i=0;i<=100;i++)
        {
           adapter.add(i);
        }
        spinnerYears.setAdapter(adapter);
        years = spinnerYears.getSelectedItemPosition();

   }//onCreate

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

  savedInstanceState.putInt("MyInt", years);


}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  Spinner spinnerYears = (Spinner) findViewById(R.id.spinYears);//Spinner
    final ArrayAdapter <Integer> adapter = new ArrayAdapter <Integer> (this,      android.R.layout.simple_spinner_item );
           adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        for(int i=0;i<=100;i++)
     {
        adapter.add(i);
     }

      spinnerYears.setAdapter(adapter);
      TextView tvYears = (TextView) findViewById(R.id.tvYears);//spinner Tv
  tvYears.setVisibility(TextView.GONE);


 int myInt = savedInstanceState.getInt("MyInt");
  spinnerYears.setSelection(myInt);//
  Toast.makeText(getBaseContext(), "Restored int"+myInt, Toast.LENGTH_LONG).show();
}
最佳回答

Because you don t handle the event of changing orientation, so everytime you change orientation, your program (or Activity) is re-created, which means the onCreate() method is called always; which leads to a result that onSaveInstanceState() and onRestoreInstanceState() never being called.

http://developer.android.com/vis/android/app/Actative.html#ConfigurationChanges”rel=“nofollow” http://developer.android.com/vis/android/app/Actative.html#ConfigurationChanges

为了确定这一点,在<代码>AndroidManifest.xml上添加了这一特性:

android:configChanges="orientation"

避免<条码>活性的重新启用,而且,与这一条完全吻合!

问题回答

onRestoreInstanceState()中,你从未将变式年<<>代码/代码>的贴现值转让给下。 您将它置于暗中,然后无视它。 因此,在<条码>上,<条码>年限/代码>。 吨位尚未确定,可节省这一未定值(0)。

1. 如果您将getInt()行文改为:

years = savedInstanceState.getInt("MyInt");
spinnerYears.setSelection(years);

它应当开始工作。 Good Luck!





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...