在我的Android应用程序中,我有三个活动。第一个活动接受用户输入并将long的值通过Intent extras传递给第二个活动,第二个活动显示内容。然后,第二个活动接受一些用户输入,并通过Intent将长数组传递给第三个活动,在此基础上显示内容。 现在,当我从第三个活动回溯到第二个活动时,我希望清除第二个活动的用户输入,这些活动存储在长数组中,但我的第二个活动从第一个活动获得的字符串值保持不变。 任何人都可以指导我,如何实现这一点。
编辑: 的 SECOND_ACTIVITY_CODE:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_details);
//get long from first activity
Bundle firstactivitybundle = getIntent().getExtras();
myLong1 = firstactivitybundle.getLong("key");
//send this long to SQLITE db and get details
Dbadapter details = new Dbadapter(this);
details.open();
data =details.getDetailedSymps(myLong1);
details.close();
//Display these details in form of checkboxes
for (int i = 0; i < data.length; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setTextColor(Color.BLACK);
cb.setButtonDrawable(id);
cb.setText(data[i]);
checkboxLayout1.addView(cb);
//saving checkbox state in an singleton class for later use
AppData.getInstance().setSensation(data[i]);
}
//second activity button
next1 = (Button) findViewById(R.id.Next1);
next1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < checkboxLayout1.getChildCount(); i++) {
if (checkboxLayout1.getChildAt(i) instanceof CheckBox) {
CheckBox cb = (CheckBox) checkboxLayout1.getChildAt(i);
if (cb.isChecked()) {
senssymp = cb.getText().toString();
//getting Ids of checkboxes that are checked from SQLITE db
Dbadapter senscheck = new Dbadapter(getApplicationContext());
senscheck.open();
sensdetailId = senscheck.getsensdetailId(senssymp);
senscheck.close();
//saving ids in a list
sensIdList.add(sensdetailId);
}
}
}
//converting list to long array
String[] getSym = sensIdList.toArray(new String[sensIdList.size()]);
long[] sens_checked_ids = new long[getSym.length];
for (int i = 0; i < getSym.length; i++) {
sens_checked_ids[i] = Long.valueOf(getSym[i]);
}
// finally create a bundle and sending the long array containg Ids to 3rd activity
Bundle bundle_sensIds = new Bundle();
bundle_sensIds.putLongArray("sens ids", sens_checked_ids);
Intent final_intent = new Intent(DetailActivity.this,FinalActivity.class);
final_intent.putExtras(bundle_sensIds);
startActivity(final_intent);
}
});
}
SOLUTION:
在我的第二个活动的onStop()中,我清除了保存所有ID的sensIdList
@Override
protected void onStop() {
super.onStop();
sens_checked_ids= new long[sens_checked_ids.length];
sensIdList.clear();
}
问题解决了。
答案 0 :(得分:1)
你可以实现这个,只需在第二个活动
的onStop方法中使用此代码 //get long from first activity
Bundle firstactivitybundle = getIntent().getExtras();
myLong1 = firstactivitybundle.getLong("key",null);
if(myLong1 !=null){
//Do your stuff
}
@Override
public void onStop() {
super.onStop();
//You can use same code just before calling your third activity
getIntent().removeExtra("key");
//OR May be try like this
getIntent().setLong("key",null);
}
由于您尚未在此处发布任何代码,因此您只需提供基本想法即可获得您的requirmnet。
答案 1 :(得分:1)
要打开新的活动实例,您可以尝试:
Intent intent = new Intent(ActivityC.this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); //this combination of flags would start a new instance even if the instance of same Activity exists.
intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
finish();
startActivity(intent);
如果你不想那样,只需要清除数据,那么你可以在onResume
的activityB中试试这个:
myArray = new String[]; //reinitialize all data structure
myArrayList= new ArrayList<String>();
sens_checked_ids = new long[];
希望它有所帮助。
答案 2 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
尝试在Second Activity中取一个静态标志(isClear)并尝试在第三个Activity中按下后面设置值true现在在onResume()中检查此标志第二个Activity on value clean you second Second data object
第二项活动
public static boolean isClear;
@Override
protected void onResume() {
if(isClear){
isClear=false;
// try to clear data here
}
super.onResume();
}
第三项活动
@Override
public void onBackPressed() {
SecondActivity.isClear=true;
super.onBackPressed();
}