androidandroid-layoutsharedpreferencesactivity-state

Android, How can i get the last activity state?


I have a one.xml file:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scrollView">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:orientation="vertical"
    android:id="@+id/lineerLayout">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/sp_info"
        android:id="@+id/label_sp_info"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/label_x" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:id="@+id/spx"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/label_y" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"
        android:id="@+id/spy" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_label_continue"
        android:id="@+id/continue_aligment" />
</LinearLayout>
</ScrollView>

and then I have two.xml file like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/nPointlinearLayout"
>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/nPointInfo" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/labelnPointX"/>
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:id="@+id/nPointX" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/labelnPointY" />
<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal"
    android:ems="10"
    android:id="@+id/nPointY" />
</LinearLayout>

I can add two.xml in one.xml when I click button using inflater. There is no problem. But when i click phone back button or exit all activity and then re entry, Adding views gone. How can i get the last activity state? with all ID's?

this is my code to click button.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.one);
    continueAligment=(Button)findViewById(R.id.continue_aligment);
    lineerLayout=(LinearLayout)findViewById(R.id.lineerLayout);
    inflater=getLayoutInflater();
    continueAligment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view=inflater.inflate(R.layout.two,lineerLayout,false);
            nPointLineerLayout=(LinearLayout)view.findViewById(
               R.id.nPointlinearLayout);
            lineerLayout.addView(nPointLineerLayout,
            lineerLayout.indexOfChild(continueAligment));
        }
    });
}

Solution

  • Add a value in shared preferences

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.one);
        continueAligment = (Button) findViewById(R.id.continue_aligment);
        lineerLayout = (LinearLayout) findViewById(R.id.lineerLayout);
        final SharedPreferences sp = PreferenceManager
            .getDefaultSharedPreferences(this);
        inflater = getLayoutInflater();
        if (sp.getBoolean("ADD_TWO", false)) {
            inflateTwo();
        } else continueAligment.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                inflateTwo();
                sp.edit().putBoolean("ADD_TWO", true).commit();
            }
        });
    }
    
    private void inflateTwo() {
        view = inflater.inflate(R.layout.two, lineerLayout, false);
        nPointLineerLayout = (LinearLayout) view
            .findViewById(R.id.nPointlinearLayout);
        lineerLayout.addView(nPointLineerLayout,
            lineerLayout.indexOfChild(continueAligment));
    }
    

    Of course if you added two already you want to disable that button - no ?