如何将数据从活动 A 传递/保存到 B,然后再传递到 B?
Posted
技术标签:
【中文标题】如何将数据从活动 A 传递/保存到 B,然后再传递到 B?【英文标题】:How to pass/save the data from activity A to B and then to B? 【发布时间】:2015-03-16 02:11:55 【问题描述】:我用 eclipse 制作了一个应用程序,它是关于按钮的,第一个按钮计算我所做的点击,第二个按钮将我发送到活动 B 和 .在活动 B 中,包括我制作的第三个按钮来购买 100 次点击。当我单击按钮购买 100 次时,我现在面临的问题是,单击 mainactivity 上的金额不是从 1000 次开始,并且不保存我在购买之前所做的最后一次单击。
主活动:
public class MainActivity extends ActionBarActivity
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),MainActivityTwo.class);
startActivityForResult(intent, count);
);
final Button button1 = (Button) findViewById(R.id.button1);
final TextView textView1 = (TextView) findViewById(R.id.textView1);
button1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Intent in = getIntent();
int cou = in.getIntExtra("", 1000);
// Get the extras (if there are any)
Bundle extras = in.getExtras();
if (extras != null)
// TODO: Do something with the value of isNew.
count++;
int sum = cou+ count;
textView1.setText("Clicked "+ sum +":times");
else
count++;
textView1.setText("Clicked"+ count +":times");
);
Activity_Two:
public class MainActivityTwo extends ActionBarActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(),MainActivity.class);
i.putExtra("", 1000);
startActivity(i);
);
【问题讨论】:
你的 OnActivityForResult 方法在哪里也发布了 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) // 检查 requestCode 是否对应你的 Second Activity。 if (requestCode == 0) // 检查结果是否正常 if (resultCode == RESULT_OK) 单击按钮时,您正在增加计数,但不会将其传递给下一个活动,并且还使用一些键名(如计数)而不是“”作为键名来在意图中添加值 【参考方案1】:试试那个代码
更新 MainActivity
public class MainActivity extends Activity
static int count = 0;
public static int MAIN_ACTIVITY = 1;
TextView textView1;
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
MainActivityTwo.class);
startActivityForResult(intent, MAIN_ACTIVITY);
);
button1 = (Button) findViewById(R.id.button1);
textView1 = (TextView) findViewById(R.id.textView1);
button1.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
count++;
textView1.setText("total clicks are => "+count);
);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == MAIN_ACTIVITY) // Check if result is ok
if (resultCode == RESULT_OK)
int clicks = data.getIntExtra("100_CLICKS", 100);
Toast.makeText(MainActivity.this,
"you bought "+clicks+" clicks : ", Toast.LENGTH_SHORT)
.show();
//now you have 100 clicks here from MainActivityTwo
int total_clicks=clicks+count;
Toast.makeText(MainActivity.this,
"Total Clicks Are : ", Toast.LENGTH_SHORT)
.show();
textView1.setText("total clicks are => "+total_clicks);
而 MainActivityTwo 是
public class MainActivityTwo extends Activity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
Intent mIntent = new Intent();
mIntent.putExtra("100_CLICKS", 100);
setResult(RESULT_OK, mIntent);
finish();
);
在你的清单中
<activity android:name=".MainActivityTwo"></activity>
【讨论】:
用我更新的 MainActivity 更改您的 MainActivity 并运行它并告诉我该代码是否有效 总点击次数 = 102 ... 在我再次开始点击后,答案是总点击次数 = 3 你的意思是在 total_Clicks=102 之后再次开始点击,计数从 0 开始,如果你按下点击按钮,然后是 1,然后是 2,然后是 3,依此类推 在 onActivityResult 下一行 textView1.setText("总点击次数 => "+total_clicks);写 count=total_clicks;【参考方案2】:使用 onSaveInstanceState 和 onRestoreInstanceState
@Override
protected void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
outState.putInt("clicks", "x") // x is the int that count clicks.
还有
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
super.onRestoreInstanceState(savedInstanceState);
x = savedInstanceState.getInt("clicks");
x + y //y is the newly bought clicks if any.
onSaveInstanceState 在 Acitivty 暂停时调用(即您将从活动 A 移动到活动 B 时。
onRestoreInstanceState 在 Activity A 恢复时被调用。也就是当你从Activity B回到Activity A时
【讨论】:
以上是关于如何将数据从活动 A 传递/保存到 B,然后再传递到 B?的主要内容,如果未能解决你的问题,请参考以下文章
将一个 mutableList 从活动 A 传递到 Kotlin 中的活动 B。
如何在 Kotlin 中将对象列表从活动 A 传递到活动 B?