虽然我知道我们可以使用 get 和 put extras 来传递给其他活动,但我只是在尝试是否可以通过接口存储和获取数据。我存储了数据,但在其他活动中,我无法获取数据。
下面是我累了。
interface PassDataToOtherActivity {
public void passData(String data);
@SuppressLint("NewApi")
default void setData(String s) {
}
public class MainActivity extends AppCompatActivity implements PassDataToOtherActivity {
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.first_text);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
textView.setText("Hello Test");
setData(textView.getText().toString());
passData(textView.getText().toString());
}
@Override
public void passData(String data) {
//override methods
}
}
public class SecondActivity extends AppCompatActivity implements PassDataToOtherActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = findViewById(R.id.textView_2);
}
@Override
public void passData(String data) { //why i am not getting the datas from previous activity?
textView.setText(data);
}
@Override
public void setData(String data) { //why I am not able to get the datas from previous activity?
textView.setText(data);
}
}
侃侃尔雅
蓝山帝景
芜湖不芜
潇湘沐
相关分类