[英]GetExtra Android won't work
我是android世界的新手,所以我开始通过创建“ Keep”应用程序进行练习。 如您所料,我可以添加注释。 问题是我在这里进行主要活动:
@Override
protected void onResume() {
super.onResume();
if (newNote) {
newNote = false;
findViewById(R.id.note)).setText(intentNewNote.getStringExtra("toto"));
}
}
public void addNotes(View view) {
newNote = true;
intentNewNote = new Intent(this, newNote.class);
startActivity(intentNewNote);
}
所以我有一个调用onClick addNotes的按钮,这是newNote类-
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.new_note);
intent = this.getIntent();
}
@Override
protected void onDestroy() {
intent.putExtra("toto", "tototookiklojlojlllllllllllllllllllllllllllllto");
super.onDestroy();
}
toto Extra是一项测试,但无法打印,因此可以正常工作
@Override
protected void onResume() {
super.onResume();
if (newNote) {
newNote = false;
intentNewNote.putExtra("toto", "dzazda");
((TextView) findViewById(R.id.note)).setText(intentNewNote.getStringExtra("toto"));
}
}
因此,当我将多余的内容放入另一个活动中时,它不起作用,唯一的解释是,我在addNotes类上得到的意图是不同的。 有一个主意吗?
谢谢。
您是否使用putExtra来保存数据? 我对您要做什么感到非常困惑。
为了创建Keep类型的应用程序,您将需要某种数据库/服务器。 查看“解析”或“ Firebase”。
我试图了解您在做什么,并且我相信您正在尝试从第二次活动中返回一些信息,但是您使用的是与您收到的意图相同的意图。 它在android上的工作方式是这样的:
从您的FirstActivity中,使用startActivityForResult()方法调用SecondActivity
例如:
意图i =新意图(this,SecondActivity.class); startActivityForResult(i,1); 在您的SecondActivity中,设置要返回到FirstActivity的数据。 如果您不想返回,请不要进行任何设置。
例如:如果要发送回数据,请在secondActivity中:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
如果您不想返回数据:
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
已编辑
现在,在FirstActivity类中,为onActivityResult()方法编写以下代码。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.