[英]GetExtra Android won't work
I'm new to the android world so I started practicing by create a "Keep" application. 我是android世界的新手,所以我开始通过创建“ Keep”应用程序进行练习。 So as you imagine I can add notes.
如您所料,我可以添加注释。 The problem is here I go my main activity here :
问题是我在这里进行主要活动:
@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);
}
So I have a button that call onClick addNotes and here is the newNote class - 所以我有一个调用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();
}
The toto Extra is a test but it can't be printed and this way it work 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"));
}
}
So when I put the extra in another activity it don't work the only explanation is that the intent I get on the addNotes class is not the same. 因此,当我将多余的内容放入另一个活动中时,它不起作用,唯一的解释是,我在addNotes类上得到的意图是不同的。 Has someone an idea ?
有一个主意吗?
Thank you. 谢谢。
Are you using putExtra in a way to save your data? 您是否使用putExtra来保存数据? I am very confused in what you are trying to do.
我对您要做什么感到非常困惑。
In order to create a Keep type of application, you will need some sort of database/server. 为了创建Keep类型的应用程序,您将需要某种数据库/服务器。 Look at Parse or Firebase.
查看“解析”或“ Firebase”。
I am trying to understand what your are doing and I believe you are trying to return some information from your second activity, but you are using the same intent that you receive. 我试图了解您在做什么,并且我相信您正在尝试从第二次活动中返回一些信息,但是您使用的是与您收到的意图相同的意图。 The way it works on android is this :
它在android上的工作方式是这样的:
From your FirstActivity call the SecondActivity using startActivityForResult() method 从您的FirstActivity中,使用startActivityForResult()方法调用SecondActivity
For example: 例如:
Intent i = new Intent(this, SecondActivity.class); 意图i =新意图(this,SecondActivity.class); startActivityForResult(i, 1);
startActivityForResult(i,1); In your SecondActivity set the data which you want to return back to FirstActivity.
在您的SecondActivity中,设置要返回到FirstActivity的数据。 If you don't want to return back, don't set any.
如果您不想返回,请不要进行任何设置。
For example: In secondActivity if you want to send back data: 例如:如果要发送回数据,请在secondActivity中:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
If you don't want to return data: 如果您不想返回数据:
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
Edited 已编辑
Now in your FirstActivity class write following code for the onActivityResult() method. 现在,在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.