how to stop activity recreation on screen orientat

2020-02-05 10:09发布

how i can stop the restarting or recalling of on create() on screen orientation ,i want to stop the recreation of activity on screen orientation. thanks in advance please tell me any better solution its really creating a problem. like in my program i am selecting some picture but on screen orientation the image goes off so thats why i want to stop the recreation of activity on screen orientation.

enter code here
public void onCreate(Bundle savedInstanceState) 
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainwindow);
    Toast.makeText(getApplicationContext(),"a", 1).show();

    bitmap = (Bitmap)getLastNonConfigurationInstance();
    //Toast.makeText(getApplicationContext(),"a1", 1).show();

    if (savedInstanceState != null) 
    {
        bitmap=BitmapFactory.decodeFile(mImageCaptureUri.getPath());
        Toast.makeText(getApplicationContext(),"preview have value", 1).show();
        preview.setVisibility(View.VISIBLE);
        From_Folder.setVisibility(View.GONE);
        From_Camera.setVisibility(View.GONE);
        preview.setImageBitmap(bitmap);


    }

标签: android
7条回答
男人必须洒脱
2楼-- · 2020-02-05 10:22

This is happening because when screen orientation rotates the Activity gets re-started. In this case you can add configChanges attribute in your tag in the AndroidManifest file to stop the re-creation of the Activity.

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

By, this also it won't stop though the orientation changes.

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.login_landscape);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.login);         
        }
    }
查看更多
唯我独甜
3楼-- · 2020-02-05 10:25

In your AndroidManifest.xml file, in the activity add

android:configChanges="keyboardHidden|orientation"

Example as below:

 <activity android:name=".YourActivity" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation">
查看更多
ら.Afraid
4楼-- · 2020-02-05 10:29

Two ways for this:

Either you can set android:configChanges="keyboardHidden|orientation" in Manifest file to avoid recreation of activity.

Or Make the changes you want to apply on changing the orientation inside the overrided method

@Override

public void onConfigurationChanged(Configuration newConfig) {

            // Perform the actions

    super.onConfigurationChanged(newConfig);

}
查看更多
够拽才男人
5楼-- · 2020-02-05 10:30

Add

android:orientation="vertical"

or

android:orientation="horizontal"

to your layout in mainwindow.xml.

Example:::

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

No need to add anything in Activity class.

Hope this may help you

查看更多
萌系小妹纸
6楼-- · 2020-02-05 10:31

There are a more full list of parameters for prevent activity recreations (in Manifest.xml):

<activity 
    android:name = ".MyActivity" 
    android:configChanges = "orientation|keyboard|keyboardHidden|screenLayout|screenSize">
</activity>
查看更多
老娘就宠你
7楼-- · 2020-02-05 10:35

Not the best, but maybe the easiest solution is to add

android:configChanges="keyboardHidden|orientation" 

to youractivity in your manifest so it looks like

<activity android:name="com.your.activity"
      android:configChanges="keyboardHidden|orientation"/>
查看更多
登录 后发表回答