2014-09-04 66 views
0

我正在嘗試將用戶輸入值從第四個活動傳遞到第五個以及第六個活動。我已經使用意圖傳遞值。但現在當我運行該應用程序時,它會從按鈕單擊跳到第六個活動,跳過第五個活動。是因爲我已經同時使用了這兩種意圖?我如何修改代碼以避免這種情況?如何避免在使用多個意圖時跳過活動?

Fourth.java

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fourth); 

    final EditText et; 
    final Button b; 

    et = (EditText) findViewById(R.id.editText1); 
    b = (Button) findViewById(R.id.button1); 

    b.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Intent intent = new Intent(Fourth.this, Fifth.class); 
      intent.putExtra("thetext", et.getText().toString()); 
      startActivity(intent); 

      Intent intentnew = new Intent(Fourth.this, Sixth.class); 
      intentnew.putExtra("thetext", et.getText().toString());    
      startActivity(intentnew); 
     } 
    } 
} 
+0

保存共享prefrence中的數據並使用任何類似於10或100個活動的地方共享數據 – 2014-09-04 13:32:35

回答

2

下面是一些你可以有選擇。

  1. 當你只想在這個時刻用Intent,這樣你就可以到第五使用Intent。而後再由第五至第六使用另一個Intent從第五活動傳遞相同的數據通過你的第四個活動的數據。
  2. 在第四項活動

所以有這個

Intent intent = new Intent(Fourth.this, Fifth.class); 
intent.putExtra("thetext", et.getText().toString()); 
startActivity(intent); 

而在你的第五,

String text = getIntent().getStringExtra("thetext"); 
Intent intentnew = new Intent(Fifth.this, Sixth.class); 
intentnew.putExtra("theSametext", text);    
startActivity(intentnew); 

2.You可以將數據保存到SharedPreferences - 使用這個你的信息保存到「偏好「文件的應用程序。 Refer this question and answer瞭解如何使用它。

3.將其寫入SQLite數據庫 - 您可以創建一個新的SQLite表,用於存儲數據並向其中寫入新行。這有更多的開銷,只有當你有大量的數據存儲在同一個應用程序時,它纔有用。你可以爲此refer this tutorial

4.您也可以創建一個Singelton class,它可以是一個靜態類,具有可設置的公共屬性。但是,這僅適用於跨多個活動臨時創建和保留數據。

所以,如果你想要使用Intent只有你可以使用第一種方法。

相關問題