Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按下时清除上一个活动数组,android_Android_Android Intent - Fatal编程技术网

按下时清除上一个活动数组,android

按下时清除上一个活动数组,android,android,android-intent,Android,Android Intent,在我的android应用程序中,我有三项活动。第一个活动接受用户输入,并将一个长直通的意图extras的值传递给第二个活动,第二个活动在此基础上显示内容。然后,第二个活动接受一些用户输入,并通过Intent将一个长数组传递给第三个活动,在此基础上显示内容。 现在,当我从第三个活动返回到第二个活动时,我希望清除存储在长数组中的第二个活动的用户输入,但第二个活动从第一个活动获得的字符串值保持原样。 谁能指导我,如何做到这一点 编辑: 第二个活动代码: @Override protected

在我的android应用程序中,我有三项活动。第一个活动接受用户输入,并将一个长直通的意图extras的值传递给第二个活动,第二个活动在此基础上显示内容。然后,第二个活动接受一些用户输入,并通过Intent将一个长数组传递给第三个活动,在此基础上显示内容。 现在,当我从第三个活动返回到第二个活动时,我希望清除存储在长数组中的第二个活动的用户输入,但第二个活动从第一个活动获得的字符串值保持原样。 谁能指导我,如何做到这一点

编辑: 第二个活动代码:

 @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);
            }
        });
        } 

问题解决了。

您可以实现这一点,只需在第二个活动的顶部方法中使用此代码即可

 //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的基本想法,因为您没有在这里发布任何代码

要打开活动的新实例,可以尝试以下操作:

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);
如果您不想这样做,只需要清除数据,那么您可以在activityB的
onResume
中尝试此操作:

myArray = new String[]; //reinitialize all data structure
myArrayList= new ArrayList<String>();
sens_checked_ids = new long[];
myArray=新字符串[]//重新初始化所有数据结构
myArrayList=新建ArrayList();
sens_checked_id=新长[];

希望有帮助。

试试这种方法,希望这能帮助你解决问题。

尝试在第二个活动中使用一个静态标志(isClear),并尝试在第三个活动中设置值true。现在,在onResume()第二个活动中检查此标志,并根据值清除第二个活动数据对象

第二项活动

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();
}
第三项活动

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();
}

尝试在第二个活动中清除一个静态标志,并尝试在第三个活动中设置值true。现在在onResume()第二个活动中检查此标志,根据值清除第二个活动数据对象。@hareshchelana!你能用一些例子解释一下吗?简单地检查一下你的长数组大小
>0
,然后在第二个活动的
onCreate()
中清除它。@Rustam!试过了,但是它给了我'nullPointerException'错误。试过了,但是无法清除我的第二个活动的长数组。当我从第三个活动返回到第二个活动时,数组包含以前用户输入的值。为什么不发布第二个活动代码,以便我们建议您适当的答案!!!。为什么在第二个活动中调用getIntent()方法?我希望我的长数组为空,而不是第一个活动的长值。谢谢你的回答。