我正在尝试将布尔值计数保存在onPause
中,并使用onResume
在SharedPreference
中进行检索。但是,当我第一次启动该应用程序时,我获得了{strong> count 的false
值。
我不知道为什么我搜索了我的问题,找不到匹配的答案。
以下是我的一项活动的代码:
public class ListView extends AppCompatActivity {
private DatabaseReference mDatabase;
private ArrayList<String> url= new ArrayList<>();
private ArrayList<String> listitems = new ArrayList<>();
private ArrayAdapter mAdapter;
private android.widget.ListView listView;
private AlertDialog.Builder builder;
private int posi;
private boolean count =true;
BroadcastReceiver onComplete;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
Log.d("count onCreate",String.valueOf(count));
sharedPreferences = getSharedPreferences("Count",0);
builder = new AlertDialog.Builder(ListView.this);
final View coordinatorLayout = findViewById(android.R.id.content);
Intent intent = getIntent();
final String uri=intent.getStringExtra("uri");
final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
mAdapter = new CustomAdapter(this,listitems);
listView = (android.widget.ListView)findViewById(R.id.listview);
mDatabase = FirebaseDatabase.getInstance().getReference().child(uri);
mDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String key = dataSnapshot.getKey();
String value= dataSnapshot.getValue(String.class);
listitems.add(0,key);
url.add(0,value);
listView.smoothScrollToPosition(0);
mAdapter.notifyDataSetChanged();
listView.setAdapter(mAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
if ((cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED) ||
(cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED)) {
builder.setMessage("Do You Want To Download "+listitems.get(position)+" "+uri+ " ?");
builder.setTitle(R.string.confirm);
builder.setIcon(R.drawable.pdf);
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
posi = position;
Intent intent1 = new Intent(ListView.this, downloadService.class);
intent1.putExtra("uri", url.get(position));
startService(intent1);
Snackbar sb = Snackbar.make(coordinatorLayout, "Downloading in Background, See Notification", Snackbar.LENGTH_SHORT);
View sbView = sb.getView();
sbView.setBackgroundColor(getResources().getColor(R.color.material_orange));
sb.show(); }
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert1 = builder.create();
alert1.show();
}
else {
final View coordinatorLayout = findViewById(android.R.id.content);
Snackbar sb = Snackbar.make(coordinatorLayout, "No Internet Connection..!", Snackbar.LENGTH_SHORT);
View sbView = sb.getView();
sbView.setBackgroundColor(getResources().getColor(R.color.material_orange));
sb.show();
}
}
});
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getApplicationContext(),"Something Went Wrong :(" ,Toast.LENGTH_SHORT).show();
}
});
onComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
AlertDialog.Builder builder = new AlertDialog.Builder(ListView.this);
builder.setTitle("Download Completed");
builder.setMessage("Do You Want To Open "+listitems.get(posi)+" "+uri+ " ?");
builder.setIcon(R.drawable.pdf);
builder.setNegativeButton("Later", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setPositiveButton("Open", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent inten = new Intent(ListView.this,onDownload.class);
inten.putExtra("uri",url.get(posi));
startService(inten);
}
});
AlertDialog alert = builder.create();
alert.show();
}
};
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(onComplete);
Log.d("count onPause",String.valueOf(count));
sharedPreferences.edit().putBoolean("count",count).apply();
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
count =sharedPreferences.getBoolean("count",true);
Log.d("count onResume",String.valueOf(count));
if (count){
count=false;
Toast.makeText(getApplicationContext(),"Loading...",Toast.LENGTH_SHORT).show();
}
}
}
Logcats
07-24 19:15:25.478 19784-19784 / com.example.mmstq.myapp D / countonCreate:true
07-24 19:15:25.489 19784-19784 / com.example.mmstq.myapp D / count onResume:假
答案 0 :(得分:0)
您的onCreate调用不会输出共享首选项的值,但会输出默认为true的count变量的值。我们不知道当时的实际值是多少,但是鉴于您的onResume输出可能是错误的。
答案 1 :(得分:0)
之所以发生这种情况,是因为当您首次运行应用程序时,onResume()
运行时,请参阅Android的“活动”生命周期,请在此处查看Android Activity Lifecycle
因此,您可以在首次加载应用程序时处理它。