2011-11-01 32 views

Trả lời

18

Trong thẻ Hoạt động của ghi manifest

android: configChanges = "định hướng | screensize"

dụ:

<activity android:name=".MainActivity" 
      android:configChanges="orientation|screenSize"> 
+10

Thận trọng: Bắt đầu với Android 3.2 (API cấp 13), "kích thước màn hình" cũng thay đổi khi thiết bị chuyển giữa hướng dọc và ngang. Do đó, nếu bạn muốn ngăn khởi động lại thời gian chạy do thay đổi định hướng khi phát triển cho API cấp 13 trở lên (như được khai báo bởi thuộc tính minSdkVersion và targetSdkVersion), bạn phải bao gồm giá trị "screenSize" ngoài giá trị "định hướng". Đó là, bạn phải decalare android: configChanges = "orientation | screenSize". (http://developer.android.com/guide/topics/resources/runtime-changes.html) – Rubix

+0

Thông tin tốt Rubix. +1 –

+0

Rubix: Định hướng và screenSize giá trị có nghĩa là gì ở đây. Câu hỏi chỉ dành cho sự tò mò của tôi. – vgokul129

1

Trong tệp manifest.xml thêm android: configChanges = "orientation" cho hoạt động.

3

Sử dụng một tấm séc cờ trong onCreate. Hãy cờ = true tại thời điểm khởi tạo/khai thêm android: configChanges = "định hướng" trong ur file manifest

Trong file java ur ghi đè onConfigurationChanged phương pháp và làm cho lá cờ như sai.

Sau khi thực hiện quá trình này, bạn sẽ được gọi nhưng mã đề cập trong số nếu sẽ không được gọi. Di chuyển mã ur bên trong điều kiện nếu.

thử sử dụng tính năng này.

static boolean flag = true; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    if(flag) 
     Log.d("ONCREATE", "flag is true"); 
}  
@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    // TODO Auto-generated method stub 
    Log.d("ONCONFIGCHANGE", "CALLED"); 
    flag = false; 
    super.onConfigurationChanged(newConfig); 
} 
3

phương pháp thay thế là sử dụng phương pháp onSaveInstanceState để lưu bất kỳ dữ liệu phi dai dẳng vào một Bundle. Tiểu bang phải được khôi phục trong số onRestoreInstanceState hoặc trong onCreate. Trong onCreate bạn phải phân tích thông số savedInstanceState và nếu nó không phải là null thì bạn nên khôi phục trạng thái đã lưu trước đó.

2

Giải pháp này là giải pháp tốt nhất hiện nay. trong file manifest của bạn thêm

<activity 
     android:configChanges="keyboardHidden|orientation|screenSize" 
     android:name="your activity name" 
     android:label="@string/app_name" 
     android:screenOrientation="landscape"> 
     </activity 

Và trong lớp hoạt động của bạn thêm đoạn mã sau

@Override 
public void onConfigurationChanged(Configuration newConfig) 
{ 
    super.onConfigurationChanged(newConfig); 
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
     //do your stuff here 
     // 
    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
    //do your stuff here 

    } 
} 
-1

Đối với C# Xamarin Tôi có giải pháp này:

using System.Collections.Concurrent; 


public static class ObjectExchanger 
{ 
    private static ConcurrentDictionary<string, object> ObjectList = new ConcurrentDictionary<string, object>(); 

    /// <summary> 
    /// Add key and object. Do not add the same key twice. 
    /// </summary> 
    /// <param name="key"></param> 
    /// <param name="obj"></param> 
    public static void Add(string key, object obj) 
    { 
     ObjectList.GetOrAdd(key, obj); 
    } 

    /// <summary> 
    /// Get object via key. Do net Get twice. Key/object will remove after get it once. 
    /// </summary> 
    /// <param name="key"></param> 
    public static object Get(string key) 
    { 
     object obj = null; 

     ObjectList.TryRemove(key, out obj); 

     return obj; 
    } 
} 

Bạn có thể sử dụng trong trong OnCreate Phương pháp sao lưu và khôi phục dữ liệu của bạn cho hoạt động sau khi xoay.

Các vấn đề liên quan