Android-当活动设置为单顶时无法检索意图
我正在开发具有以下场景的应用程序:Android-当活动设置为单顶时无法检索意图,android,android-intent,android-activity,android-task,Android,Android Intent,Android Activity,Android Task,我正在开发具有以下场景的应用程序: Activity A -> Activity B -> Activity C -> Activity A 我通过Intents在每个Activity之间传递我的值,它可以工作,但是当我回到ActivityA时,似乎所有的值都被清除了 来自活动的代码C: Intent client = new Intent(getApplicationContext(), ActivityA.class); client.putExt
Activity A -> Activity B -> Activity C -> Activity A
我通过Intent
s在每个Activity
之间传递我的值,它可以工作,但是当我回到Activity
A时,似乎所有的值都被清除了
来自活动的代码
C:
Intent client = new Intent(getApplicationContext(), ActivityA.class);
client.putExtra("TOKEN_VALUE", TOKEN_VALUE);
client.putExtra("PARAMS", params); // Hashmap with {"id":"value"}
client.putExtra("PICLIST", picArray);
client.putExtra("TESTLIST", testArray);
client.putExtra("TESTDATA", myData);
client.putExtra("TESTTEMP", myTemp);
client.putExtra("COMPTEUR", compteur);
client.putExtra("LAUNCH",first_launch);
client.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(client);
当我试图获取这些值时,它们都是null
编辑:
以下是onCreate()
和onNewIntent()
的代码:
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//初始化gps以获取位置
locationListener=新建MyLocationListener();
locationMangaer=(LocationManager)getSystemService(Context.LOCATION\u服务);
locationMangaer.RequestLocationUpdate(LocationManager.GPS_提供程序,5000,10,locationListener);
//检查是否启用了Wifi
最终WifiManager wifi=(WifiManager)this.getSystemService(Context.wifi_服务);
如果(iShareWiFi(wifi)){
新建AlertDialog.Builder(此)
.setCancelable(错误)
.setMessage(“L”应用程序需要连接互联网)
.setPositiveButton(“附件辅助参数wifi”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
startActivity(新意图(设置、动作和设置));
}
})
.setNegativeButton(“手机辅助参数”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface对话框,int-id){
startActivity(新意图(设置、动作、数据、漫游、设置));
}
})
.setIcon(android.R.drawable.ic_对话框_警报)
.show().getWindow().setLayout(1000600);
}
mId=(编辑文本)findViewById(R.id.exam_阀);
mName=(EditText)findViewById(R.id.exam_op);
b1=(按钮)findViewById(R.id.take_picture);
b2=(按钮)findViewById(R.id.add\u检查按钮);
b3=(按钮)findViewById(R.id.addTest_按钮);
b1.设置ClickListener(单击ListenerBoutons);
//初始化并填充两个微调器
条件\状态=(微调器)findViewById(R.id.微调器\阀门\条件);
原因\状态=(微调器)findViewById(R.id.Spinner\检查\原因);
ArrayAdapter valve\u condition\u adapter=新的ArrayAdapter(这是android.R.layout.simple\u spinner\u项){
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
返回super.getView(position、convertView、parent);
}
};
阀门\条件\适配器.setDropDownViewResource(android.R.layout.simple \微调器\下拉菜单\项);
valve_condition_adapter.add(getResources().getString(R.string.ok));
条件数组[0]=“确定”;
阀门条件适配器.add(getResources().getString(R.string.rusty));
条件数组[1]=“生锈”;
阀门条件适配器添加(getResources().getString(R.string.damaged));
conditionArray[2]=“已损坏”;
valve_condition_adapter.add(getResources().getString(R.string.breaked));
conditionArray[3]=“已断开”;
状态。设置适配器(阀门状态适配器);
ArrayAdapter检查\u原因\u适配器=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项){
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
返回super.getView(position、convertView、parent);
}
};
检查\u原因\u适配器.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉项);
检查原因适配器.add(getResources().getString(R.string.programmed));
reasonArray[0]=“已编程”;
检查原因适配器.add(getResources().getString(R.string.valve\u安装));
reasonArray[1]=“阀门安装”;
检查原因适配器.add(getResources().getString(R.string.improvised));
reasonArray[2]=“即兴创作”;
原因\状态。设置适配器(检查\原因\适配器);
条件_status.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
条件=条件数组[位置];
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
原因\状态。setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
原因=原因数组[位置];
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
//初始化请求队列
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
mRequestQueue.start();
//创建令牌以连接到REST服务器
最后一个字符串myUrl=我的服务器地址;
JsonObjectRequest=newJSONObjectRequest(request.Method.GET,myUrl,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject JSONObject){
exponent=jsonObject.optString(“publicExponent”);
模数=jsonObject.optString(“publicModule”);
ex=新的大整数(指数);
mo=新的大整数(模数);
Log.i(“testrest”,例如toString());
Log.i(“testrest”,mo.toString());
试一试{
key=RSACrypt.encrypt(“临时密码”,mo,ex);
Log.i(“令牌生成”,“La cléest:+密钥”);
HashMap params=新的HashMap();
参数put(“密码”,密钥);
String body=newJSONObject(params.toString().replace(“\\”,”);
Log.i(“JSON对象”,正文);
JsonRequest
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// init the gps to get a location
locationListener = new MyLocationListener();
locationMangaer = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationMangaer.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
// Checking if Wifi is enabled
final WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (isSharingWiFi(wifi)) {
new AlertDialog.Builder(this)
.setCancelable(false)
.setMessage("L' application requiert une connexion à Internet")
.setPositiveButton("Accéder aux paramètres wifi ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
})
.setNegativeButton("Accéder aux paramètres du réseau mobile", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS));
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show().getWindow().setLayout(1000, 600);
}
mId = (EditText) findViewById(R.id.exam_valve);
mName = (EditText) findViewById(R.id.exam_op);
b1 = (Button) findViewById(R.id.take_picture);
b2 = (Button) findViewById(R.id.add_exam_button);
b3 = (Button) findViewById(R.id.addTest_button);
b1.setOnClickListener(clickListenerBoutons);
// Init and fill the two spinners
condition_status = (Spinner) findViewById(R.id.spinner_valve_condition);
reason_status = (Spinner) findViewById(R.id.spinner_exam_reason);
ArrayAdapter<String> valve_condition_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
};
valve_condition_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
valve_condition_adapter.add(getResources().getString(R.string.ok));
conditionArray[0] = "ok";
valve_condition_adapter.add(getResources().getString(R.string.rusty));
conditionArray[1] = "rusty";
valve_condition_adapter.add(getResources().getString(R.string.damaged));
conditionArray[2] = "damaged";
valve_condition_adapter.add(getResources().getString(R.string.broken));
conditionArray[3] = "broken";
condition_status.setAdapter(valve_condition_adapter);
ArrayAdapter<String> exam_reason_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
};
exam_reason_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
exam_reason_adapter.add(getResources().getString(R.string.programmed));
reasonArray[0] = "programmed";
exam_reason_adapter.add(getResources().getString(R.string.valve_installation));
reasonArray[1] = "valve-installation";
exam_reason_adapter.add(getResources().getString(R.string.improvised));
reasonArray[2] = "improvised";
reason_status.setAdapter(exam_reason_adapter);
condition_status.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
condition = conditionArray[position];
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
reason_status.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
reason = reasonArray[position];
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// Init the request queue
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
mRequestQueue.start();
// Creating the Token to connect to the REST server
final String myUrl = MY_SERVER_ADDRESS;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, myUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
exponent = jsonObject.optString("publicExponent");
modulus = jsonObject.optString("publicModulus");
ex = new BigInteger(exponent);
mo = new BigInteger(modulus);
Log.i("TEST REST ", ex.toString());
Log.i("TEST REST ", mo.toString());
try {
key = RSACrypt.encrypt("temporary password", mo, ex);
Log.i("TOKEN GENERATION", "La clé est : " + key);
HashMap<String, String> params = new HashMap<String, String>();
params.put("password", key);
String body = new JSONObject(params).toString().replace("\\", "");
Log.i("JSON OBJECT", body);
JsonRequest tokenRequest = new JsonStringRequest(Request.Method.POST, myUrl, body, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
TOKEN_VALUE = jsonObject.optString("token");
Log.i("TOKEN GENERATION", "Le token est : " + TOKEN_VALUE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
String message = new String(volleyError.networkResponse.data);
Log.i("REQUETE 2", "Erreur" + volleyError.getMessage() + message);
}
});
mRequestQueue.add(tokenRequest);
} catch (Exception e) {
e.printStackTrace();
Log.i("ERROR TOKEN GENERATION", e.getMessage());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
TOKEN_VALUE = "HOSTUNREACHABLE";
Log.i("REQUETE 1", "Erreur" + volleyError.getMessage());
}
});
mRequestQueue.add(request);
// Sending the request with all parameters
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Getting the current date
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss");
String date = df.format(Calendar.getInstance().getTime());
lon = locationMangaer.getLastKnownLocation(LOCATION_SERVICE).getLongitude();
lat = locationMangaer.getLastKnownLocation(LOCATION_SERVICE).getLatitude();
// Filling params
params.put(REASON, reason);
params.put(ID_VALVE, mId.getText().toString());
params.put(OPERATOR, mName.getText().toString());
params.put(DATE, date + "T00:00:00");
params.put(CONDITION, condition);
params.put(LONGITUDE, Double.toString(lon));
params.put(LATITUDE, Double.toString(lat));
for (int k = 0; k < pictureNameArray.length; k++) {
Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory() + "/MyApp/MyImages/" + pictureNameArray[k] + ".jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.NO_WRAP);
Log.i("PICTURE data", encodedImage);
HashMap<String, String> currentPic = new HashMap<String, String>();
currentPic.put(DATA, encodedImage);
currentPic.put(FILENAME, pictureNameArray[k]);
currentPic.put(MEDIATYPE, "image/jpg");
PicList.put(new JSONObject(currentPic));
}
AlertDialog.Builder alert = new AlertDialog.Builder(HomeActivity.this);
final EditText edittext = new EditText(getApplicationContext());
alert.setMessage("Veuillez saisir un commentaire sur l'examen");
alert.setTitle("Commentaire");
alert.setView(edittext);
alert.setPositiveButton("Continuer", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String myComment = edittext.getText().toString();
params.put(COMMENT, myComment);
if (TOKEN_VALUE == "HOSTUNREACHABLE") {
// Save on SQL Lite + alert
} else {
Intent intent = new Intent(HomeActivity.this, SendMyRequestService.class);
intent.putExtra("PARAMS", params);
intent.putExtra("PICLIST", pictureNameArray);
intent.putExtra("TESTLIST", TestList.toString());
startActivity(intent);
}
}
});
alert.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
params.clear();
}
});
alert.show();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
params.put(REASON, reason);
params.put(ID_VALVE, mId.getText().toString());
params.put(OPERATOR, mName.getText().toString());
params.put(CONDITION, condition);
Intent intent = new Intent(HomeActivity.this, ListDeviceActivity.class);
intent.putExtra("COMPTEUR", pictureNumber);
intent.putExtra("TOKEN_VALUE", TOKEN_VALUE);
intent.putExtra("PARAMS", params);
intent.putExtra("PICLIST", pictureNameArray);
intent.putExtra("TESTLIST", TestList.toString());
startActivity(intent);
}
});
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String TOKEN_VALUE = intent.getStringExtra("TOKEN_VALUE");
String params = intent.getStringExtra("params");
String test_data = intent.getStringExtra("TESTDATA");
String test_temp = intent.getStringExtra("TESTTEMP");
String testArray = intent.getStringExtra("TESTLIST");
try {
JSONObject parameters = new JSONObject(params);
Log.i("Id valve récuperé", parameters.optString("ID_VALVE"));
Log.i("Raison récuperée", parameters.optString("REASON"));
Log.i("Nom opérateur récuperé", parameters.optString("OPERATOR"));
Log.i("Condition récuperée", parameters.optString("CONDITION"));
if (parameters.optString("ID_VALVE") != null)
mId.setText(parameters.optString("ID_VALVE"));
if (parameters.optString("REASON") != null) {
int i = this.getResources().getIdentifier(parameters.optString("REASON"), "string", this.getPackageName());
reason_status.setSelection(i);
}
if (parameters.optString("OPERATOR") != null)
mName.setText(parameters.optString("OPERATOR"));
if (parameters.optString("CONDITION") != null) {
int j = this.getResources().getIdentifier(parameters.optString("CONDITION"), "string", this.getPackageName());
condition_status.setSelection(j);
}
} catch (JSONException e) {
e.printStackTrace();
}
if (intent.getStringArrayExtra("PICLIST") != null) {
pictureNameArray = intent.getStringArrayExtra("PICLIST");
}
HashMap<String, String> currentTest = new HashMap<String, String>();
if (testArray != null) {
try {
TestList = new JSONArray(testArray);
currentTest.put(TDATA, test_data);
currentTest.put(TEMP, test_temp);
TestList.put(new JSONObject(currentTest));
} catch (JSONException e) {
e.printStackTrace();
}
} else {
currentTest.put(TDATA, test_data);
currentTest.put(TEMP, test_temp);
TestList.put(new JSONObject(currentTest));
}
}
client.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
//do some action
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String TOKEN_VALUE = intent.getStringExtra("TOKEN_VALUE");
String params = intent.getStringExtra("params");
// and so on ....
}
Intent i = new Intent(ActivityC.this, ActivityA.class);
i.putExtra("TOKEN_VALUE", TOKEN_VALUE);
i.putExtra("PARAMS", params);
startActivity(i);
Intent intent= getIntent();
String value1 = intent.getStringExtra("TOKEN_VALUE");
String value2 = intent.getStringExtra("params");