I have an simple activity that binds some data to a listView via an ArrayAdapter. I put a toast in my onCreate. If I leave the phone alone for a few seconds-minutes, the toast shows again. I'm totally clueless as of how this is happening. I'm posting my code below. Maybe I'm missing something integral to the application that I was unaware of.
package com.eghdk.myapp.gui;
import java.util.ArrayList;
import android.annotation.TargetApi;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;
import com.eghdk.myapp.R;
import com.eghdk.myapp.adapters.MyAdapter;
import com.eghdk.myapp.util.AppUtil;
import com.eghdk.myapp.util.MyPost;
public class ActivityMyBlog extends ListActivity {
MyAdapter adapter;
ArrayList<MyPost> myPostArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_blog);
// Show the Up button in the action bar.
setupActionBar();
if (!AppUtil.isNetworkAvailable(this)) {
Log.d("", "No network.");
} else {
Log.d("", "Connection detected. Will try to load from web.");
loadDataFromWeb(1, 40);
}
if (adapter == null) {
Toast.makeText(this, "adapter is null", 0).show();
} else {
}
}
@Override
public Object getLastNonConfigurationInstance() {
return (getListAdapter());
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.activity_my_blog, menu);
// return true;
// }
//
// @Override
// public boolean onOptionsItemSelected(MenuItem item) {
// switch (item.getItemId()) {
// case android.R.id.home:
// // This ID represents the Home or Up button. In the case of this
// // activity, the Up button is shown. Use NavUtils to allow users
// // to navigate up one level in the application structure. For
// // more details, see the Navigation pattern on Android Design:
// //
// //
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
// //
// NavUtils.navigateUpFromSameTask(this);
// return true;
// }
// return super.onOptionsItemSelected(item);
// }
public void loadDataFromWeb(int page, int count) {
myPostArrayList = new ArrayList<MyPost>();
Ion.with(
ActivityMyBlog.this,
"http://myblog.com/api/get_posts/?page=" + page + "&count="
+ count).asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
//Edited this json code out for easier reading
myPost.setAttachmenUrl("http://myblog.com/img.png");
myPost.setPostTitle("Title");
myPost.setPostContent("Content");
myPost.setPostUrl("http://myblog.com/mypost");
myPost.setAuthorName("EGHDK");
myPostArrayList.add(myPost);
}
// END OF EVERY POST LOOP
Log.d("DEBUG", "4");
adapter = new MyAdapter(ActivityMyBlog.this,
R.layout.row, myPostArrayList);
setListAdapter(adapter);
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(this, ActivityMyBlogDetail.class);
intent.putExtra("atUrl", myPostArrayList.get(position)
.getAttachmenUrl());
intent.putExtra("content", myPostArrayList.get(position)
.getPostContent());
intent.putExtra("title", myPostArrayList.get(position)
.getPostTitle());
intent.putExtra("url", myPostArrayList.get(position).getPostUrl());
startActivity(intent);
}
}
Here is my isNetworkAvailable method:
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
boolean isAvailable = false;
if (networkInfo != null && networkInfo.isConnected()) {
isAvailable = true;
}
return isAvailable;
}
Here is my getView method from my ArrayAdapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, parent, false);
}
((TextView) convertView.findViewById(R.id.titleTextView)).setText(Html
.fromHtml(postsArrayList.get(position).getPostTitle()));
ImageView image = (ImageView) convertView
.findViewById(R.id.postImageView);
//Library to help with loading of images
UrlImageViewHelper.setUrlDrawable(image, postsArrayList.get(position)
.getAttachmenUrl());
((TextView) convertView.findViewById(R.id.subTextView)).setText(Html
.fromHtml(postsArrayList.get(position).getAuthorName()));
return convertView;
}
Update:
I got another phone to use as a stop watch and tried this two times.
Both times I force closed the app.
I opened the app and the screen got dim at 2 Minutes and 45 Seconds later I got a toast.
I opened the app and the screen 48 seconds later I got the toast.
This is NOT right. I'm sure the android application lifecycle doesn't work like this. Must be another problem. I'm using this on my Android 4.4 LG G2.
Update 2:
Inserting a log
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d("STOP", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
}
Results:
01-20 04:42:51.792: D/STOP(4817): XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
01-20 04:42:51.852: D/(4817): No network. Will use values from database.
01-20 04:42:51.922: I/ActivityManager(4817): Timeline: Activity_idle id: android.os.BinderProxy@420465c0 time:15096283
01-20 04:42:52.432: D/STOP(4817): XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
01-20 04:42:52.512: D/(4817): No network. Will use values from database.