Color change on click [duplicate] - java

I am having trouble saving the state/singleton of my application.
When the application starts a loading screen (activity) is shown and a singleton is initialized with values from a webservice call (note that network access cannot run on the main thread).
After the singleton is created I open my main activity. Note that values from the singleton are required to build the layout.
Now assume the app goes in the background and is killed there (e.g. because of low memory). My singleton instance is deleted as the app is killed. When I switch back to my app it tries to recreate the main activity. As I mentioned earlier the values from the singleton are required to build the layout, so this leads to a NullPointerException (when I try to access members of the singleton, as it is not there anymore).
Can I somehow tell android to start the first loading activity after the app was killed? It would be great if I could refresh the singleton before the layout is recreated, but this seems to be a problem as network calls can not be on the main thread and therefore not block until the refresh is finished.
I assume that I could save the singleton in all activities onStop and recreate it in the onCreate methods, but this seems a bit too unpredictable and would probably lead to a inconsistent state...
Another way could be to just always finish my activity onStop, but this would lead to losing on which tab the user last and so on, even if the app is not killed, so this is not a good option.
Any ideas on how to solve this?

Why not just use a SharedPreferences instead of a singleton?
Anytime you want to save some global state, commit it to preferences. Anytime you want to read the global state, read it back from preferences.
Then you don't have to concern yourself with application lifecycle at all, as your data will always be preserved regardless of what the phone is doing.

For something like that I used a pseudo singelton object as a Application class. This object will be created on the beginning and will be in the memory. But note that the system will terminate the application if the memory is needed by other applications. However this object is persitent even if all activities are temporally terminated.
To use that you need to declare that in your android manifest like here:
<application android:label="#string/app_name"
android:icon="#drawable/icon"
android:description="#string/desc"
android:name=".MySingeltonClass"
...
Here is a code example:
public abstract class MySingeltonClass extends Application {
// ...
public void informClientOnline() {
clientOnline=true;
Log.v(LOG_TAG, "Client is online!");
}
public void informClientShutdown() {
clientOnline=false;
Log.v(LOG_TAG, "Client is going offline. Waiting for restart...");
Timer t=new Timer("shutdowntimer", false);
t.schedule(new TimerTask() {
#Override
public void run() {
if(!clientOnline) {
Log.v(LOG_TAG, "Client has not restartet! Shutting down framework.");
shutdown();
System.exit(0);
}
}
}, 5000);
}
}
this two functions are called like this:
((MySingeltonClass)getApplicationContext()).informClientOnline();

You could save your Singleton when onSaveInstanceState() in the Activity gets called. All you need to do is to make it implement Parcelable (it's Androids own form of serialization), then you can put it in the outState Bundle in onSaveInstanceState() which will allow you to retrieve it laver in onCreate() or onRestoreInstanceState() in the Activity, whichever you like.
I've included an example for you:
public class TestActivity extends Activity {
private MySingleton singleton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState.containsKey("singleton")) {
singleton = savedInstanceState.getParcelable("singleton");
} else {
singleton = MySingleton.getInstance(5);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("singleton", singleton);
}
public static class MySingleton implements Parcelable {
private static MySingleton instance;
private int myData;
private MySingleton(int data) {
myData = data;
}
public static MySingleton getInstance(int initdata) {
if(instance == null) {
instance = new MySingleton(initdata);
}
return instance;
}
public static final Parcelable.Creator<MySingleton> CREATOR = new Creator<TestActivity.MySingleton>() {
#Override
public MySingleton[] newArray(int size) {
return new MySingleton[size];
}
#Override
public MySingleton createFromParcel(Parcel source) {
return new MySingleton(source.readInt());
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(myData);
}
}
}

Related

How to communicate between Activity and Threads created by other Actvity

When you have 2 activities ( A and B ) and A is the MainActivity, now you start your App and A starts B.
B is a activity with a Dialog interacting with the user, creating a Bluetooth Connection and 2 Threads, 1 Receiving and one Sending.
Now, what is the best way to send the Information from A to the threads of B and the other way round?
First I used a static WeakReference, but I heard this causes a lot of problems, so I want to ask for a more common solution.
Please keep in mind, when starting an Activity from another Activity, you can only pass Serializable Objs and simple data. So it is not possible to use a Handler that way.
Here is the static WeakReference I used:
public class T1 extends Thread{
private static WeakReference<T1> weak_T1;
public void T1 (){
weak_T1 = new WeakReference<T1> (This);
}
public static WeakReference getWeakReverence() {
return weak_T1;
}
}
Here is a way to look for a running Thread in the stack:
for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (thread.getName().equalsIgnoreCase("T1")){
T1A =thread;
}else if (thread.getName().equalsIgnoreCase("T2")){
T2A =thread;
}
}
Also possible solution:
public class example extends Thread {
private static example instance;
private example() {
}
public static example getIsntance(){
if(instance == null){
instance = new example();
}
return instance;
}
}
A WeakReference probably isn't what you want, here. That is, presuming that your Thread object either does not terminate, or somehow maintains some information useful to Activity A after Activity B has stopped. If you use a WeakReference, it might become "null" as soon as Activity B ends, and the thread terminates. Just use regular old strong references. It'll ensure T1, and the information contains, continues to exist until you're done with it.
public class ActivityB extends Activity
{
private T1 t1;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
t1 = startMyThread();
}
#Override
public void onBackPressed()
{
ActivityA.tempT1 = t1;
//This technique presumes that Activity A is guaranteed to resume after a
//back button press, based on the arrangement of your backstack, etc. If
//Activity A is started via some other means (e.g., an explicit startActivity(),
//finish(), etc.), then this reference will have to be set prior to
//that call, as well, in order to establish the appropriate "happens before" relationship.
//If you fail to ensure that Activity A resumes after this point, you will
//risk a memory leak.
super.onBackPressed();
}
}
public class ActivityA extends Activity
{
public static T1 tempT1 = null;
private T1 t1;
#Override
public void onResume()
{
super.onResume();
if(tempT1 == null)
{
//Apparently, Activity B hasn't executed yet. Provide the user with a button to start it.
}
else
{
t1 = tempT1;
tempT1 = null; //To avoid a memory leak
//We just retrieved the reference that Activity B left for us.
//Now, change UI states so that the user can see information about t1.
}
}
}

Clear stack activities from specific activity with eventBus

I would like to know if it's possible to clear all activities from an old one. I would like to use enventBus to do this.
Example of a stack of activities:
startActivity(A) then startActivity(B) then startActivity(C) then startActivity(D)...
Activity B is registered onEvent(ClearStackFromHere()) with eventBus.
And from Activity D I want to post the event post(new ClearStackFromHere) with eventBus too.
So, is it possible to clear the stack of activities from B ?
What should I write inside my ClearStackFromHere().
Thanks,
I have implemented a similar solution in one of my projects.
What I needed was a way to keep only the most recent 3 activities in the back stack, and clear the others before them. This only applies to a certain Navigation flow within my application where it becomes possible that an infinite amount of Activities can be added to the back stack.
e.g. A opens B - which opens C, C can then open another instance of A or B... etc.
I should note that this solution uses EventBus 2.4.0 and there may be a better way to implement it with 3.0+.
First off, I defined a helper called ActivityTracker. It keeps track of what Activities are currently active, as well as an identifier for each activity. It also has methods that can be called to finish all activities in the back stack except for the most recent n amount.
public class ActivityTracker {
private static ArrayList<String> activityStack = new ArrayList<>();
//Notify the Tracker of a new Activity to track
public static void activityActive(String uuid){
addToBackStack(uuid);
}
//Notify the tracker of an Activity that should no longer be tracked
public static void finishing(String uuid){
removeFromBackStack(uuid);
}
//Call this to clear entire back stack
public static void killAllBackStackActivities(){
killPreviousActivities(0);
}
//Call this to clear back stack while keeping most recent X amount
private static void killPreviousActivities(int keepAmount){
if(activityStack.size() <= keepAmount) {
return;
}
//Copy to not manipulate while looping.
String[] tempList = activityStack.toArray(new String[activityStack.size()]);
int counter = activityStack.size();
for(String id : tempList){
if(counter == keepAmount){
return;
}
counter--;
//Send notification to kill specific activity
EventBus.getDefault().post(new ActivityShouldDieEvent(id));
}
}
private static void addToBackStack(String uuid){
if(!activityStack.contains(uuid)){
activityStack.add(uuid);
killPreviousActivities(3); //Always kill all activities except most recent 3.
}
}
private static void removeFromBackStack(String uuid){
if(activityStack.contains(uuid))
activityStack.remove(uuid);
}
}
Then, I defined a subclass of AppCompatActivity called BackStackTrackActivity. All relevant Activities in the app extend this class. The subclass looks like this:
public class BackStackTrackActivity extends AppCompatActivity {
//Random ID for activity to be identified by
protected String uuid = UUID.randomUUID().toString();
//Receive notification that activity should finish
public void onEvent(ActivityShouldDieEvent ev){
if(ev.getUuid().equals(this.uuid)){
finish();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
//Unregister from EventBus
EventBus.getDefault().unregister(this);
//Tell tracker to stop tracking
ActivityTracker.finishing(uuid);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Register for events
EventBus.getDefault().register(this);
//Tell tracker to track activity
ActivityTracker.activityActive(uuid);
}
}
With some work, I think you will be able to adapt this solution into something that meets your needs.
I hope that helps.

IntentService Concurrency

I have got a class that extends IntentService, let's say class A, and then I have got 2 more classes as class B and class C, these classes extends class A. The reason is because I want to initialize everything in class A so that code would be reused in class B and C. So class B and C's onHandleIntent method looks like:
#Override
protected void onHandleIntent(Intent intent) {
super.onHandleIntent(intent);
//Specific init stuff
}
What I want to do is simply do some concurrency checks at class A's either constructor or onHandleIntent. For example when method x is being used(doesn't matter which class uses it) I would like to be able to flag this method being used. Sometimes one method schedules another thread by using Handler.postDelayed, so I want to make sure that flag is kept as in use until the thead work is finished.
So far it sounds like it can be done easily by shared singleton, however each of these classes have their own AlarmManager which extends BroadcastReceiver, so using a shared singleton doesn't really work as lifecycle existence of a singleton is not guaranteed.
The only solution that I can think of is using a database or local file system, which sounds quite silly. So is there another way to flag concurrent methods among different IntentService that are being triggered by AlarmManager?
To give a clear image I'd like to give an example of class A, class B and class C
public class A extends IntentService {
public A(String name) {
super(name);
//initialise objects here
}
#Override
protected void onHandleIntent(Intent intent) {
//initialise objects that uses intent here
}
protected synchronized void methodA() {
//wait until flagB is free
flagA = in_use; //Flag something as being in use here
//change some objects here
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//do some stuff with objects
flagB = not_in_use; //Flag something as being free here
}
}, (1000 * 45));// 45secs
}
protected synchronized void methodB() {
if (flagA == not_in_use) {
flagB = in_use;
//do some stuff with objects
flagB = not_in_use;
}
else {
//do something else
}
}
public class B extends IntentService {
public A(String name) {
super("B");//Super initializes everything
}
#Override
protected void onHandleIntent(Intent intent) {//This will run every 30 minutes
super.onHandleIntent(intent);
methodA();
}
}
public class C extends IntentService {
public A(String name) {
super("C");//Super initializes everything
}
#Override
protected void onHandleIntent(Intent intent) {//This will run every 45 minutes
super.onHandleIntent(intent);
methodB();
}
}
So basically class B and C doesn't contain any private classes. What i want to do is just like the example above, flag things.
The problem is as I mentioned if it is just a static field, then static field might not stay in the memory as Android OS can kill anything inactive for saving memory.
What I want to do is simply do some concurrency checks at class A's either constructor
Don't block in a constructor. Creating an object should be a free operation. In case of IntentService you would probably get an ANR because the object is constructed from within the main thread.
or onHandleIntent
Yes. That would be a place where you could do that because that's where something actually happens. And it is executed within a background thread so blocking here is ok.
To synchronize between multiple instances of a class (including subclasses) you will need to use something static because that's the only common thing that multiple instances "share".
Synchronizing outside of the lifetime of your app is also (probably) not required. If your app process is killed and restarted from AlarmManager you would have a new singleton to work with. Anything that happened before is dead and so nothing can happen in parallel.
For example synchronize using the .class object of A
public class AService extends IntentService {
public AService() {
super("AService");
}
#Override
protected void onHandleIntent(Intent intent) {
synchronized (AService.class) {
// magic happens here.
}
}
}
class BService extends AService {
void foo() {
synchronized(AService.class) {
// we can't be in here if another intentservice is within above block.
}
}
}
That does work as long as those services are running within the same app process (i.e. every simple app that does not define extra processes). static is globally accessible everywhere in the app.
External locking using e.g. the filesystem would be required if you have multiple processes since they can't access each other's objects at all.
besides synchronized, look into java.util.concurrent for more powerful / better ways of synchronizing. E.g. `Semaphore and ReentrantLock (roughly equivalent to synchronized)
I want to make sure that flag is kept as in use until the thead work is finished.
If you app is killed, you'll have to restart the work if it did not finish. So keeping a "work-progress-flag" beyond the lifetime of your app makes no sense. And static (e.g. your singleton) works fine while it is running.

Own Listener change UI-Thread in Android

#first Sorry for my bad english.
I have created a own Listener. I want to change a TextView, when the Listener is called in the MainActivity from a Service. The idea for my own Listener is from:
http://tseng-blog.nge-web.net/blog/2009/02/17/how-implement-your-own-listener-android-java/
In the Code Example the TriggerMethod() ist called from a Calculation Thread, running in the Service.
I solved the Problem, but I find, it isn't pretty nice, because in every new Activity I have to make a new Thread. Is it possible to create an interface/listener that automatically can change the UI?
Used to solve the Problem:
http://developer.android.com/guide/components/processes-and-threads.html
ResultListener.java:
public interface ResultListener {
public void onResultAvailable(double result);
}
SimuService.java:
public class SimuService extends Service {
private ResultListener mResultListener = null;
public void setResultListener(ResultListener listener){
mResultListener=listener;
}
public void triggerMethode(){
observeResultDouble=getObserveDouble;
mResultListener.onResultAvailable(observeResultDouble);
}
MainActivity:
public class MainActivity extends FragmentActivity{
TextView txtView;
ResultListener mResultListener;
SimuService mSimuService;
protected void onCreate(Bundle savedInstanceState) {
txtView = (TextView) findViewById(R.id.txtServiceTime);
//Create Service .....an Bind
mResultListener = new ResultListener() {
#Override
public void onResultAvailable(double result) {
txtView.setText("Result: "+result);
}
};
mSimuService.setResultListener(mResultListener);
}
MY SOLUTION:
ResultListener = new ResultListener() {
#Override
public void onResultAvailable(double result) {
this.result=result;
runOnUiThread(setNewDataToUI);
}
};
private Thread setNewDataToUI = new Thread(new Runnable() {
#Override
public void run() {
txtView.setText("Result: "+result);
}
});
First of all: If you reference a Service in an Activity, the Service becomes pretty much useless. The advantage of services are, that they are loose coupled and can work indepenendtly form activities (=what the user sees) and its lifecycle and might even be in their own process. Thus activity-service communication is through intents or inter-process language AIDL, not through callbacks. If you want something executed asynchronosly use AsyncTask.
To your main problem: as you found out, you can only modify the UI on the UI-thread. So by design, leave changing UI in the component, thats responsibly for that (either activtiy or fragment), that will prevent the need of runOnUiThread()
Your code seems like txtView.setText("Result: "+result); will be executed in the Activity, but it wont. It will be executed in the Service, which (as I impleied before) does not run on the UI-thread. The problem is, I dont get the intent, what exactly you want to achieve so it is hard to give you an alternative solution.

How can I tell if my context is still valid?

I'm working with a fairly common situation right now - download some data over the web, then update a view to display it. Clearly, I want to do the web download in the background, and then update the view on the main UI thread. Now looking at my code, I'm a little worried about my Activity and its UI elements being killed off before I update them. Here's the essence of what I have in mind:
Thread update = new Thread() {
public void run() {
final Data newData = requestData();
if (newData != null) {
post(new Runnable() {
public void run() {
Toast.makeText(MyClass.this, "I'll do things here that depend on my context and views being valid", Toast.LENGTH_SHORT).show();
}
});
}
}
};
update.start();
It seems possible that while I'm downloading data, the activity may be destroyed. What happens then? Will my thread continue to execute? Will I end up trying to access dead objects?
Usually I do this by AsycTask, but the work seemed simple enough this time to just inline the threads-launching-threads stuff. Will I make things any better by using an AsyncTask instead?
If your Context is an Activity, you can check if it is finishing or has finished with the isFinishing() method:
if ( context instanceof Activity ) {
Activity activity = (Activity)context;
if ( activity.isFinishing() ) {
return;
}
}
Toast.makeText(context, "I'll do things here that depend on my context and views being valid", Toast.LENGTH_SHORT).show();
What you really want to use is an AsyncTaskLoader. These are my new favorite classes in the Android API. I use them all the time and they were made to solve problems just like this. You won't have to worry about when to stop your download or anything like that. All the threading logic is taken care of for you, including telling the thread to stop if the activity has been closed. Just say what it is you want to do in the loadInBackground() method. Note that if you are developing for an API lower than 3.0, you can still access all the loaders via the Android Support Package.
If you use anonymous classes, they will have an internal reference to the outer class, so it's not like it becomes inaccessible all of a sudden because other references have been cleared. AsyncTask actually doesn't change anything, it uses similar mechanics for notifying about results.
You can use loaders, they are designed to be in sync with the activity lifecycle. They are available only since Android 3.0, but you can use support package to work with them on any device with 1.6 or later.
There is even a simpler solution, you can just use a boolean field which indicates whether activity has gone away. You should set this field in onPause() (or whenever you think you won't need the notifications anymore) and check for it when you show toast. You won't even have to use synchronization, since this field is confined to the main thread, so it's absolutely safe. By the way, if you change this field somewhere else than in onDestroy(), don't forget to add a statement which resets your field back in the counterpart method.
public class MyActivity extends Activity {
private boolean activityDestroyed = false;
#Override
protected void onDestroy() {
activityDestroyed = true;
}
private void updateData() {
new Thread() {
#Override
public void run() {
final Data newData = requestData();
if (newData == null) return;
runOnUiThread(new Runnable() {
public void run() {
if (activityDestroyed) return;
Toast.makeText(MyActivity.this, "Blah",
Toast.LENGTH_SHORT).show();
}
});
}
}.start();
}
}
I usually use Weak Reference to avoid leaking context in views
Weak Reference for Context
private var mContext: WeakReference<Context?>? = null
Assign Context
mContext = WeakReference(appContext)
Get Context
mContext .get()
Validate Context
if (mContext?.get() is Activity &&
(mContext?.get() as Activity).isFinishing){
return
}
Kurtis is right. However, if you REALLY want to keep it simple, you can try this:
class MyActivity extends Activity {
static MyActivity context;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
MyActivity.context = this;
}
#Override
public void onDestroy() {
super.onDestroy();
MyActivity.context = null;
}
}
And then you just use MyActivity.context in your class (and check for null there). If you want the toast to not even show up when your app is in the background, use onPause/onResume instead.
Again, this is the quick and lazy approach. AsyncTask or AsyncTaskLoader is how you should be doing things.

Categories