0

У меня есть one.xml файл:Android, Как я могу получить последнее состояние активности?

<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> 

, а затем у меня есть two.xml файл как этот

<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> 

Я могу добавить two.xml в one.xml, когда я нажимаю кнопку с помощью Inflater. Нет проблем. Но когда я нажимаю кнопку «Назад» или выхожу из всех действий, а затем «Записывать», «Добавление просмотров». Как я могу получить последнее состояние активности? со всеми идентификаторами?

это мой код для нажатия кнопки.

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)); 
     } 
    }); 
} 
+0

Вы решили свою проблему? –

+0

Большое спасибо. Это нормально для одного. Я хочу добавить, что захочу. – Buartu

ответ

1

Добавить значение в общих настройках

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)); 
} 

Конечно, если вы добавили два уже вы хотите отключить эту кнопку - нет?