gpt4 book ai didi

Android 后退按钮 - 从 Intent 中丢失信息

转载 作者:行者123 更新时间:2023-11-29 00:07:01 26 4
gpt4 key购买 nike

刚接触android,所以我真的不知道这里发生了什么;

现在,在我的主要 Activity 中,我有一个下拉微调器;单击一个按钮会转到第二个 Activity ,其中有一个 ListView ,其中包含来自下拉微调器内容的信息。

当我单击另一个按钮并转到第三个 Activity ,然后按返回键时,下拉微调器内容中的信息不再存在;取而代之的是空值。

有人知道可能是什么问题吗?后退按钮是否发送 Intent ?

第二个 Activity :

public class classList extends AppCompatActivity {
ListView listView;
SimpleCursorAdapter mAdapter;
static private String[] classes = {"class1","class2"};

String course;
String number;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
course = intent.getStringExtra("com.naomi.classAlert.course");
number = intent.getStringExtra("com.naomi.classAlert.number");

Toast.makeText(getApplicationContext(), course+number,
Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_class_list);

listView = (ListView) findViewById(R.id.list);



String[] values = new String[] {
course+number+" 001", course+number+" 002", course+number+" 003"
};

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_2, android.R.id.text1, values);
listView.setAdapter(adapter);

// ListView Item Click Listener
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

// ListView Clicked item index
int itemPosition = position;

// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);

// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();

Intent intent1 = new Intent(classList.this, pickSection.class);
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
startActivity(intent1);

}

});
}

}

第三个 Activity :

public class pickSection extends AppCompatActivity {
String course;
String number;
int section;
//SharedPreferences faves = PreferenceManager
// .getDefaultSharedPreferences(this);
SharedPreferences faves;
SharedPreferences.Editor editor;

Button button;
TextView courseInfo;
TextView capacity;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
faves = getSharedPreferences("preferencename",0);
editor = faves.edit();
setContentView(R.layout.activity_pick_section);

button = (Button) findViewById(R.id.favouriteButton);
courseInfo = (TextView) findViewById(R.id.courseinfo);
capacity = (TextView) findViewById(R.id.capacity);
Intent intent2 = getIntent();
course = intent2.getStringExtra("com.naomi.classAlert.classList.course");
number = intent2.getStringExtra("com.naomi.classAlert.classList.number");
section = intent2.getIntExtra("com.naomi.classAlert.classList.section",0);

courseInfo.setText(course+number+" "+section);
capacity.setText("dummy text");


String exists = faves.getString(course+number+section,"not found");
if (!exists.equals("not found")){
button.setText("Remove from favourites");
}

}

public void addToFavourites(View v) {
String exists = faves.getString(course+number+section,"not found");
if (!exists.equals("not found")) { //it exists in favourites
editor.remove(course + number + section);
button.setText("Add to favourites");
} else {
editor.putString(course+number+section, "favourite");
button.setText("Remove from favourites");
}
editor.commit();
}
}

最佳答案

可以通过手机上的后退按钮关闭 Activity 。在这种情况下,执行 finish() 方法。如果 Activity 是通过调用 startActivity(Intent) 方法启动的,则调用者不需要来自现在已关闭的 Activity 的任何结果或反馈。

如果您使用 startActivityForResult() 方法调用启动 Activity ,您会期望来自子 Activity 的反馈。子 Activity 结束后,将调用子 Activity 的 onActivityResult() 方法,您可以根据结果执行操作。

 public void onItemClick (....){
// start pickSection
Intent intent1 = new Intent(classList.this, pickSection.class);
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
startActivity(intent1);
startActivityForResult(intent1, 0);
}

当按下后退按钮时,pickSection 完成,它可以通过 Intent 将数据发送回其调用者。这是在 finish() 方法中完成的。@覆盖

public void finish() {
Intent intent1 = new Intent();
intent1.putExtra("com.naomi.classAlert.classList.course",course);
intent1.putExtra("com.naomi.classAlert.classList.number", number);
intent1.putExtra("com.naomi.classAlert.classList.section",position+1);
// Activity finished ok, return the data
setResult(RESULT_OK, intent1);
super.finish();
}

一旦 pickSection 完成,就会调用 classList 中的 onActivityResult() 方法。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 0) {
if (data.hasExtra("com.naomi.classAlert.classList.course")) {
Toast.makeText(this, data.getExtras().getString("com.naomi.classAlert.classList.course"),
Toast.LENGTH_SHORT).show();
}
}
}

关于Android 后退按钮 - 从 Intent 中丢失信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33115362/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com