简体   繁体   中英

How to handle Screen Orientation change in android without onConfigChanged

I am new to android coding, So i'm sure there must be some stupid mistake in my code. I am trying to make a TO DO LIST app (without using Listview) and I don't know how to handle orientation change in Android. When i rotate the device all my data is lost. If you can suggest how I can go about it, I would be really grateful.

Here is my source code:

MainActivity.java

public class MainActivity extends AppCompatActivity {

LinearLayout mlistlayout;
String toDo;
private ArrayList<Entry> mlist;

static class Entry {
    String S;
    boolean b;
    public Entry(String S, boolean b) {
    }
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mlistlayout = (LinearLayout) findViewById(R.id.list);
    mlist = (ArrayList<Entry>) getLastCustomNonConfigurationInstance();
    if (mlist == null) {
        mlist = new ArrayList<>();
    }

    Button sub = (Button) findViewById(R.id.buttonsubmit);
    sub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText editText1 = (EditText)        `
            findViewById(R.id.editText1);
            toDo = editText1.getText().toString().trim();
            if (toDo.isEmpty()) {
                return;
            }
            editText1.setText("");
            AddListEntry(toDo);
        }
    });
}
@Override
public Object onRetainCustomNonConfigurationInstance() {
    return mlist;
}

void AddListEntry(String S) {
    LayoutInflater inflater = LayoutInflater.from(this);
    View v = inflater.inflate(R.layout.row, mlistlayout, false);
    final Entry entry = new Entry(S, false);
    TextView t = (TextView) v.findViewById(R.id.task_title);
    t.setText(toDo);
    CheckBox cb=(CheckBox) v.findViewById(R.id.task_delete);
    cb.setOnCheckedChangeListener(new  CompoundButton.OnCheckedChangeListener() {
         @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             entry.b=isChecked;
           }
        });
    mlist.add(entry);
    mlistlayout.addView(v);
}

}

Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bag"
tools:context="calpoly.edu.todolist.MainActivity"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<EditText
    android:inputType="text"
    android:id="@+id/editText1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:layout_weight="2" />

<Button
    android:id="@+id/buttonsubmit"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="@string/submit" />

</LinearLayout>

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

    <LinearLayout
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"></LinearLayout>
</ScrollView>

row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linear_layout"
android:orientation="horizontal" >

<CheckBox
    android:id="@+id/task_delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<TextView
    android:id="@+id/task_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:textSize="20sp" />

Save the state of the activity you would like to reuse later in savedInstanceState. And then you check if the savedInstanceState == null in onCreate. If it is null then you create the activity if not you get the state. This will show you how to do it with examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM