为什么 Savedinstance 值在返回到 Marshmallow 中的 activity 时变为空值?
Why Savedinstance values get null when returning back to that activity in Marshmallow?
我创建了一个应用程序,它从以前的 activity 中获取值作为附加值,并在 activity 中使用该值。然后将这些值发送到另一个 activity。但是当我从移动 activity 返回到之前的 activity 时,额外的值变为空值。
例如,我从 Activity A 到 Activity B (一些 id 和图像id 等)现在,我将该值作为 Intent extras 发送到 Activity C。在 Activity C 中,我得到了值(初始情况)!现在,当我按回 Activity B 并再次移动到 Activity C 时,我没有得到值( Activity C 中的一些 ID 和图像 ID 等)。这只发生在 Marshmallow 中。在 Activity 中,C 名称从 Activity B 中的服务器获取并相应移动!这在棒棒糖之前一直有效!但这发生在 Marshmallow!
我的ActivityB Fetchservices(这里移到另一个Activity代码是:
public void fetchServices(){
mProgressBar.setVisibility(View.VISIBLE);
String android_id = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ANDROID_ID);
String userid = prefs.getString("userId","0");
Log.e("USERID",userid);
Log.e("URL TOP UP", Constants.BASE_URL_SERVICE_LIST+"?deviceid="+android_id+"&userid="+userid +"&country="+countryname+"&countryid="+countryid);
RestClientHelper.getInstance().get(Constants.BASE_URL_SERVICE_LIST+"?deviceid="+android_id+"&userid="+userid+"&country="+countryname+"&countryid="+countryid, new RestClientHelper.RestClientListener() {
@Override
public void onSuccess(String response) {
Log.e("Resposnse",response);
mProgressBar.setVisibility(View.GONE);
parseResult(response);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Get item at position
GridItem item = (GridItem) parent.getItemAtPosition(position);
String myactivity = "com.mobeeloadpartner."+item.getGlobalActivity();
if(item.getGlobalActivity().equals("0") || item.getGlobalActivity() == null || ("").equals(item.getGlobalActivity())){
activity = Constants.getActivityClass("ComingSoon");
}
else{
activity = Constants.getActivityClass(item.getGlobalActivity());
}
Intent intent = new Intent(GlobalActivity.this, activity);
Log.e("Activity",item.getGlobalActivity());
intent.putExtra("country", countryname);
intent.putExtra("countryid", countryid);
intent.putExtra("countrycode", countrycode);
intent.putExtra("title", item.getTitle());
intent.putExtra("image", item.getImage());
intent.putExtra("serviceid", item.getServiceId());
//Start details activity
startActivity(intent);
}
});
}
@Override
public void onError(String error) {
}
});
}
Activity 创建代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.international_topup);
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar);
prefs = new PreferenceHelper(InternationalTopup.this);
loading = (CircleProgressBar) findViewById(R.id.loading);
check = new CheckInterNetConnection(InternationalTopup.this);
mGridView = (GridView) findViewById(R.id.gridView);
loading.setVisibility(View.INVISIBLE);
//this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
mGridData = new ArrayList<>();
mGridAdapter = new EloadGridViewAdapter(this, R.layout.grid_eload_amount, mGridData);
mGridView.setAdapter(mGridAdapter);
pd = new ProgressDialog(InternationalTopup.this);
isInternetPresent = check.isConnectingToInternet();
popup = (LinearLayout) findViewById(R.id.popup);
maintable = (TableLayout)findViewById(R.id.maintable);
tl = (TableLayout) findViewById(R.id.maintable);
noOps = (RelativeLayout) findViewById(R.id.noOps);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
countryname =null;
countryid = null;
countrycode = null;
} else {
countryname= extras.getString("country");
countryid= extras.getString("countryid");
countrycode= extras.getString("countrycode");
}
} else {
countryname= (String) savedInstanceState.getSerializable("country");
countryid= (String) savedInstanceState.getSerializable("countryid");
countrycode = (String) savedInstanceState.getSerializable("countrycode");
}
opimage = (ImageView)findViewById(R.id.opimage);
try {
countryid = countryid.toLowerCase();
}
catch(Exception e){
countryid = "0";
}
Picasso.with(getApplicationContext()).load(Constants.URL+"/app/countries/png250px/"+countryid+".png").fit().error(R.drawable.mobeeloadicon).into(opimage);
amount = (EditText)findViewById(R.id.amount);
amount.setText("0");
EditText mytext = (EditText)findViewById(R.id.phonenumber);
// mytext.setText(countrycode);
EditText code = (EditText)findViewById(R.id.code);
code.setText(countrycode);
code.setKeyListener(null);
mytext.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
operatorName = "Auto Fetch";
mGridAdapter.clear();
}
});
amount.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
localValue = "";
}
});
TextView countryName = (TextView)findViewById(R.id.countryname);
countryName.setText(countryname);
//amount.setEnabled(false);
if (isInternetPresent) {
} else {
Constants.showAlert(InternationalTopup.this,"Please check your internet connection and try again");
// SnackbarManager.show(Snackbar.with(InternationalTopup.this).text("Please check your internet connection and try again"));
}
}
请帮忙解决这个问题!
是啊!!那是一个愚蠢的错误!在开发人员选项中,有一个选项可以在移动到活动时删除 activity 数据!它以某种方式打开了!保持关闭状态!
我创建了一个应用程序,它从以前的 activity 中获取值作为附加值,并在 activity 中使用该值。然后将这些值发送到另一个 activity。但是当我从移动 activity 返回到之前的 activity 时,额外的值变为空值。
例如,我从 Activity A 到 Activity B (一些 id 和图像id 等)现在,我将该值作为 Intent extras 发送到 Activity C。在 Activity C 中,我得到了值(初始情况)!现在,当我按回 Activity B 并再次移动到 Activity C 时,我没有得到值( Activity C 中的一些 ID 和图像 ID 等)。这只发生在 Marshmallow 中。在 Activity 中,C 名称从 Activity B 中的服务器获取并相应移动!这在棒棒糖之前一直有效!但这发生在 Marshmallow!
我的ActivityB Fetchservices(这里移到另一个Activity代码是:
public void fetchServices(){
mProgressBar.setVisibility(View.VISIBLE);
String android_id = Settings.Secure.getString(getContentResolver(),
Settings.Secure.ANDROID_ID);
String userid = prefs.getString("userId","0");
Log.e("USERID",userid);
Log.e("URL TOP UP", Constants.BASE_URL_SERVICE_LIST+"?deviceid="+android_id+"&userid="+userid +"&country="+countryname+"&countryid="+countryid);
RestClientHelper.getInstance().get(Constants.BASE_URL_SERVICE_LIST+"?deviceid="+android_id+"&userid="+userid+"&country="+countryname+"&countryid="+countryid, new RestClientHelper.RestClientListener() {
@Override
public void onSuccess(String response) {
Log.e("Resposnse",response);
mProgressBar.setVisibility(View.GONE);
parseResult(response);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Get item at position
GridItem item = (GridItem) parent.getItemAtPosition(position);
String myactivity = "com.mobeeloadpartner."+item.getGlobalActivity();
if(item.getGlobalActivity().equals("0") || item.getGlobalActivity() == null || ("").equals(item.getGlobalActivity())){
activity = Constants.getActivityClass("ComingSoon");
}
else{
activity = Constants.getActivityClass(item.getGlobalActivity());
}
Intent intent = new Intent(GlobalActivity.this, activity);
Log.e("Activity",item.getGlobalActivity());
intent.putExtra("country", countryname);
intent.putExtra("countryid", countryid);
intent.putExtra("countrycode", countrycode);
intent.putExtra("title", item.getTitle());
intent.putExtra("image", item.getImage());
intent.putExtra("serviceid", item.getServiceId());
//Start details activity
startActivity(intent);
}
});
}
@Override
public void onError(String error) {
}
});
}
Activity 创建代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
setContentView(R.layout.international_topup);
toolbar = (Toolbar) findViewById(R.id.tool_bar); // Attaching the layout to the toolbar object
setSupportActionBar(toolbar);
prefs = new PreferenceHelper(InternationalTopup.this);
loading = (CircleProgressBar) findViewById(R.id.loading);
check = new CheckInterNetConnection(InternationalTopup.this);
mGridView = (GridView) findViewById(R.id.gridView);
loading.setVisibility(View.INVISIBLE);
//this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
mGridData = new ArrayList<>();
mGridAdapter = new EloadGridViewAdapter(this, R.layout.grid_eload_amount, mGridData);
mGridView.setAdapter(mGridAdapter);
pd = new ProgressDialog(InternationalTopup.this);
isInternetPresent = check.isConnectingToInternet();
popup = (LinearLayout) findViewById(R.id.popup);
maintable = (TableLayout)findViewById(R.id.maintable);
tl = (TableLayout) findViewById(R.id.maintable);
noOps = (RelativeLayout) findViewById(R.id.noOps);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
countryname =null;
countryid = null;
countrycode = null;
} else {
countryname= extras.getString("country");
countryid= extras.getString("countryid");
countrycode= extras.getString("countrycode");
}
} else {
countryname= (String) savedInstanceState.getSerializable("country");
countryid= (String) savedInstanceState.getSerializable("countryid");
countrycode = (String) savedInstanceState.getSerializable("countrycode");
}
opimage = (ImageView)findViewById(R.id.opimage);
try {
countryid = countryid.toLowerCase();
}
catch(Exception e){
countryid = "0";
}
Picasso.with(getApplicationContext()).load(Constants.URL+"/app/countries/png250px/"+countryid+".png").fit().error(R.drawable.mobeeloadicon).into(opimage);
amount = (EditText)findViewById(R.id.amount);
amount.setText("0");
EditText mytext = (EditText)findViewById(R.id.phonenumber);
// mytext.setText(countrycode);
EditText code = (EditText)findViewById(R.id.code);
code.setText(countrycode);
code.setKeyListener(null);
mytext.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
operatorName = "Auto Fetch";
mGridAdapter.clear();
}
});
amount.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
localValue = "";
}
});
TextView countryName = (TextView)findViewById(R.id.countryname);
countryName.setText(countryname);
//amount.setEnabled(false);
if (isInternetPresent) {
} else {
Constants.showAlert(InternationalTopup.this,"Please check your internet connection and try again");
// SnackbarManager.show(Snackbar.with(InternationalTopup.this).text("Please check your internet connection and try again"));
}
}
请帮忙解决这个问题!
是啊!!那是一个愚蠢的错误!在开发人员选项中,有一个选项可以在移动到活动时删除 activity 数据!它以某种方式打开了!保持关闭状态!