简体   繁体   中英

Send data from one activity to another

I want sent the content of the variable indicatorpositionstart of main2Activity to mainActivity. I used the solution "extra", but does not work.

What's the problem?

    public class Main2Activity extends AppCompatActivity {

private Button imageValidate;
int indicatorPositionStart = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

imageValidate = findViewById(R.id.buttonValidate);

imageValidate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent2 = new Intent(Main2Activity.this, MainActivity.class);
intent2.putExtra("indicatorPositionStartt", indicatorPositionStart );
startActivity(intent2);
}
});

}

    public class MainActivity extends AppCompatActivity{

int id = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView textView = findViewById(R.id.textView3);

if (id != 0) {
int id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
String indicatorPositionStart = String.valueOf(id);

textView.setText(indicatorPositionStart);
}
}

}

Thank you in advance.

The problem is that you are checking the value before you get them from the extras, see this lines on the second activity:

// you are using it here, before getting the value
if (id != 0) {
int id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
String indicatorPositionStart = String.valueOf(id);

textView.setText(indicatorPositionStart);
}
}

Another problem on the MainActivity is that you are not filling in the id field. I fixed it, just replace this one with your second activity:

    public class MainActivity extends AppCompatActivity{

    int id = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = findViewById(R.id.textView3);

    // get the value first
    id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);

    // then use it
    if (id != 0) {
        String indicatorPositionStart = String.valueOf(id);
        textView.setText(indicatorPositionStart);
    }
  }

}

In your MainActivity, remove this check if (id !=0 ) because activity started you set the value of id =0 so if (id !=0 ) this if is never true.

For Sending Data:

Intent intent = new Intent(this, SecondActivity.class); 
intent.putExtra("arg", YourData); 
startActivity(intent);

For Receiving Data:

String data = getIntent().getExtras().getString("arg");

Update

remove if( id != 0) and use if(getIntent().hasExtra("indicatorPositionStartt"))

like this:

if (getIntent().hasExtra("indicatorPositionStartt")) {
int id = getIntent().getExtras().getInt("indicatorPositionStartt", 0);
String indicatorPositionStart = String.valueOf(id);

textView.setText(indicatorPositionStart);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM