2016-09-05 36 views
0

我一直在用一個教程系列的幫助來構建的筆記應用程序中遇到問題。我剛剛創建了一個對話框,允許用戶更改音符的類別,但只要方向改變,新類別就會丟失。通過教程的指示,我重寫onSavedInstance到第一,但由於某種原因,保存的信息不解決下面的代碼:在android studio中處理方向變化

package com.workingprogess.notebook; 


    import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.support.v7.app.AlertDialog; 
    import android.util.Log; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.ImageButton; 

    public class NoteEditFragment extends Fragment { 

    private ImageButton noteCatButton; 
    private EditText title; 
    private EditText message; 
    private Button saveButton; 
    private Note.Category savedButtonCategory; 
    private AlertDialog categoryDialogObject; 
    private AlertDialog confirmDialogObject; 

    private static final String MODIFIED_CATEGORY="Modified Category"; 


    public NoteEditFragment() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     if(savedInstanceState!=null){ 
      savedButtonCategory = (Note.Category) savedInstanceState.get(MODIFIED_CATEGORY); 
     } 

     //grab layout and assign to view so that we may access widgets 
     View fragmentLayout = inflater.inflate(R.layout.fragment_note_edit, container, false); 

     //grab widget references 
     title = (EditText) fragmentLayout.findViewById(R.id.editNoteTitle); 
     message = (EditText) fragmentLayout.findViewById(R.id.editMessage); 
     noteCatButton = (ImageButton) fragmentLayout.findViewById(R.id.editNoteButton); 
     saveButton = (Button) fragmentLayout.findViewById(R.id.saveNoteButton); 


     //populate with note data 
     Intent intent = getActivity().getIntent(); 

     title.setText(intent.getExtras().getString(MainActivity.NOTE_TITLE_EXTRA)); 
     message.setText(intent.getExtras().getString(MainActivity.NOTE_MESSAGE_EXTRA)); 


     if(savedButtonCategory !=null){ 
      Log.d("not null","the new image should be carried over"); 
      noteCatButton.setImageResource(Note.categoryToDrawable(savedButtonCategory)); 
     } else { 
      Note.Category noteCat = (Note.Category) intent.getSerializableExtra(MainActivity.NOTE_CATEGORY_EXTRA); 
      savedButtonCategory = noteCat; 
      noteCatButton.setImageResource(Note.categoryToDrawable(noteCat)); 
      Log.e("null","pull from intent"); 
     } 


    //set onclick listeners 
     buildCategoryDialog(); 
     noteCatButton.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick(View v){ 
       categoryDialogObject.show(); 
      } 
     }); 

     buildConfirmDialog(); 
     saveButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       confirmDialogObject.show(); 
      } 
     }); 



     // Inflate the layout for this fragment 
     return fragmentLayout; 
    } 

    //save info before orientation change. 
    @Override 
    public void onSaveInstanceState(Bundle savedInstanceState){ 
     super.onSaveInstanceState(savedInstanceState); 
     Log.d("save","info is saved"); 
     savedInstanceState.putSerializable(MODIFIED_CATEGORY, savedButtonCategory); 
    } 

    //build pop uo dialog to change note info 
    private void buildCategoryDialog(){ 

     final String[] categories = new String[]{"Personal","Technical","Quote","Finance"}; 
     final AlertDialog.Builder categoryBuilder = new AlertDialog.Builder(getActivity()); 
     categoryBuilder.setTitle("Choose Note Type"); 

     categoryBuilder.setSingleChoiceItems(categories, 0, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int item) { 

       switch (item){ 
        case 0: 
         savedButtonCategory= Note.Category.PERSONAL; 
         noteCatButton.setImageResource(R.drawable.p_icon); 
         break; 
        case 1: 
         savedButtonCategory=Note.Category.TECHNICAL; 
         noteCatButton.setImageResource(R.drawable.t_icon); 
         break; 
        case 2: 
         savedButtonCategory=Note.Category.QUOTE; 
         noteCatButton.setImageResource(R.drawable.q_icon); 
         break; 
        case 3: 
         savedButtonCategory=Note.Category.FINANCE; 
         noteCatButton.setImageResource(R.drawable.f_icon); 
         break; 

       } 
       categoryDialogObject.cancel(); 

      } 
     }); 

     categoryDialogObject=categoryBuilder.create(); 

    } 

    private void buildConfirmDialog(){ 
     final AlertDialog.Builder confirmBuilder = new AlertDialog.Builder(getActivity()); 
     confirmBuilder.setTitle("are you sure?"); 
     confirmBuilder.setMessage("are you sure you want to save this note"); 

     confirmBuilder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       Log.d("Save Note","Note title " + title.getText()+ "Note message " 
        + message.getText()+" note category" + savedButtonCategory); 
       Intent intent = new Intent(getActivity(),MainActivity.class); 
       startActivity(intent); 
      } 
     }); 
     confirmBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       savedButtonCategory=Note.Category.TECHNICAL; 
      } 
     }); 

     confirmDialogObject = confirmBuilder.create(); 

    } 
} 
+0

嘗試在Activity - Manifest文件中添加(android:configChanges =「orientation | screenSize」),以便它不會重新運行方向更改的活動 - 請點擊https://developer.android.com/guide/topics/ resources/runtime-changes.html – Tasos

回答

0

打開應用程序清單文件和特定活動,添加以下代碼:

<activity 
     android:name=".MainActivity" //Activity where the problem is occuring 
     android:configChanges="screenLayout|orientation|screenSize"> 

其更好地把它添加「機器人:configChanges」到每一次活動保持數據狀態,即使方向變化。

+0

感謝一堆!這解決了我的問題。非常感謝有一段時間在那裏掙扎 –

+0

很好,它解決了你的問題,如果可能的話,把它標記爲**接受答案**,以便有類似問題的人可以找到它的幫助和可靠。 – DsD

0

你檢查,如果是的onSaveInstanceState得到真正叫?這個回調只會在持有碎片的Activity被破壞時纔會被調用。所以,首先檢查你的Activity是否被破壞了。

+0

是的,我用日誌來檢查它每次屏幕更改時調用三次,但添加「android:configChanges:清單解決了問題 –