如何将pass/save数据从activityA传到B再传到B?

How to pass/save the data from activity A to B and then to B?

我用 eclipse 制作了一个应用程序,它是关于按钮的,第一个按钮计算我所做的点击,第二个按钮将我发送到 activity B 和 。在 activity 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);
        }
    });


}
}

试试那个代码

已更新 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>

使用 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 暂停时调用(即当您将从 activity A 移动到 activity B.

onRestoreInstanceState 在 Activity A 恢复时调用。也就是说,当你从 Activity B 回到 Activity A