2017-12-18 195 views
-1

我有三项活动假设为ActivityA,ActivityBActivityC 其中A已经启动B和B启动C然后B被销毁。
现在我需要将一些数据从ActivityC转移回ActivityA在无关活动之间传输数据

如何在不使用Android的SharedPreference或数据库的情况下实现此目的?

+0

您可以随时使用[Intent.FLAG_ACTIVITY_NEW_TASK](https://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK)和[Intent.FLAG_ACTIVITY_CLEAR_TOP](https://developer.android。 com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP)作为您想使用'putExtra()'应用的额外数据的意图标志。 –

回答

2

让一个Constant.class并宣布串

public static String your_value = ""; 
现在在你的活动Ç

。像这样存储你的价值。

Constant.your_value = "123456"; 

并在您的活动A。像这样访问。

Log.d(getClass().getName(), "Your constant value "+Constant.your_value); 
+0

'并在您的活动A中。像这样访问。“。什么时候? – greenapps

+0

正如OP提到的那样@greenapps现在,我必须将一些数据传回到ActivityA ** ,因此每当他想访问数据时 –

+0

活动A将如何/何时知道数据可用? – greenapps

-1

使用setResult()方法用于将数据发送回

ActivityC使用此。

Intent intent = new Intent(); 
intent.putExtra("data", "yourdata") 
setResult(RESULT_OK, intent);   
finish(); 

现在,在您activityA覆盖方法onActivityResult()

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 1) { 
     if(resultCode == RESULT_OK) { 
      //get your data here 
      String strEditText = data.getStringExtra("data"); 
     }  
    } 
} 

编码愉快!