I am trying to fetch some API data to my app and I have two activities. The first one is a Splash Screen (like those used by google while your app gets loaded) and I want to know where to call finish() to end the activity.
public class SplashScreen extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AsyncDataFetch fetch = new AsyncDataFetch();
fetch.setContext(this);
fetch.execute();
}
}
I have moved all my code in my AsyncTask so I don't block the ui thread and now I can't call finish() there, or I don't know how.
Why would I call finish for my app there instead of calling it on my activity you might ask... It is because it generates some sort of glitch if I do this, because my API fetch takes about 1 second and showing and closing this activity takes less.
So, where should I call finish() and how?
You should call finish() in onPostExecute of Async Task after getting result from doInBackground().
If your activity you wanna finish is a splash srceen, you should put finish() at postExecute(), which should be overriden in your asyncsTask.
Related
I want to access the activity lifecycle method of a different activity from the present one... Can i do that? for example i have 2 activities A and B. I want to access the onStop method of activity A from activity B. can i do that? i'm trying to check the online of a user in my app which has multiple activities so i want to write code which is like = If onStop/onDestroy method of both the activities are called show that the user is offline... The code im using is
#Override
public void onStart(){
super.onStart();
mDatabaseReference.child("Online").setValue(true);
}
#Override
public void onStop(){
super.onStop();
mDatabaseReference.child("Online").setValue(false);
}
Can someone please help me out
Use Application.ActivityLifecycleCallbacks in your Application class. This way you just need to register your activities to the callbacks and from the application class only you can track down wheather any activity is present or not.
For more info please refer to this answer
To set the value You can use SharedPreferences. Declare the instance of sharedpreference at application level.
In Activity A and B you can set the value in onStop(), onDestroy() and onStart() block.
I have created a class that is extending from CountDownTimer, It has a a method onFinish() which calls when timer expires.
There are 6 Activities, user can be in any activity when timer expires, So in CounterTimerwhen Finish() method calls , i need to show an Alert Message to the user,along with i need to redirect user to Login page.
Things getting confusing, as i cannot call Intent class in the Normal Class, I can also not pass the context, as user can be in any activity.
I have written following code, but its not helping out.
I am using context here, but its giving error message on passing context to Intent
public class CounterClass extends CountDownTimer implements ITMServiceEvent {
#Override
public void onFinish() {
if(sql_code.equalsIgnoreCase("0")) {
String resultCode = command1.getString("result");
context.startActivity(context.getApplicationContext(), MainActivity.class);
}
Calling Timer at the Start of Wizard, in a Fragment
CounterClass counterClass= new CounterClass(180000,1000);
counterClass.setTextView(tvTimer);
counterClass.start();
There are two parts of your question, first is how you can clean up the Activity stack and start a new Activity on Top of them, I suppose this would be the LoginActivity in your case.
To do this, you need to set the Flag of your LoginActivity Intent when you want to start it,
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
And the second part is, you want to be able to finish the current activity after showing a dialog to the user. I assume your Timer is a Service Class, which runs in the background. The way to tell your current activity that the Time is Up ! is to send a Broadcast Message. Preferably, LocalBroadcastManager can help you out. You can have a BaseActivity class where all of your 6 Activities can be extended from it and you can register/unregister LocalBroadcastManager to/from those activities in the BaseActivity class (register in onResume and unregister in onPause). After you register them you just need to implement and handle the onReceive method where you can show a dialog and start the LoginActivity after finishing the current one.
If I run an async call onResume is there a good way to detect that if the async call is running to not call it again?
Because I am doing a network call using an AsynTask in onResume of a Fragment and it seems to be called too often
Update
THe problem is that when onResume is called there are no variables available. So I don't have a valid reference of an async task anymore to check the status.
The code is like:
public void onResume() {
AsynTask<Void, Void, String> theTask = new AsyncTask<>() {
//code to run
};
theTask.execute();
//code
}
So how am I suppose to do this since I don't have a reference? I mean where should I keep theTask in order to be able to cancel it onResume?
You can getStatus to get the status of the AsyncTask. To check if its running you can compare it to AsyncTask.Status.RUNNING.
I starting intent service in onCreate() method in subclass of Application class. But it is called when app is opened the first time. It should not calling on every time the app is opened. It does even not work when I kill the background process. And my observation its working in galaxy note but not in samsung s duos, please help me.
public class MyApp extends Application {
#Override
public void onCreate() {
Toast.makeText(this.getApplicationContext(), "APPLICATION", Toast.LENGTH_LONG).show();
context=this.getApplicationContext();
if (!ContactUtiles.isFileExit()) {
createDatabase(Opration.NOT_FOR_DELTA);
if (DBConstants.LOGD) {Log.d(TAG, DBConstants.DB_NOT_EXIST);}
}
super.onCreate();
}
}
You should read this about the Android Acitvity Lifecycle: http://developer.android.com/training/basics/activity-lifecycle/index.html
In short: onCreate() will be called when the Activity has to be created.
If you launch an app, tha Activity will have to be created. If you go to background, the Activity will most probably not be destriyed until certain time passes, so when you return to the app there will be no need to create the Activity (it wasn't destroyed).
If you want your code to execute every time, you should user onResume(). onResume() is called every time before the Activity is showed.
I started learning how to create an app on Android. I have a bit of knowledge on Java already, and now I'm trying some of the activity events.
I know how to use onCreate or onCreateOptionsMenu, and now I'm testing out onStop:
#Override
public void onStop(){
//a function that simply creates and return an AlertDialog.Builder object
displayPopup("Event", "Stopped", "OK").show();
}
I thought this would work since there's no error at compile time. But when I try to exit the app, I expect a popup dialog would show up but instead the app just crashed. It turns out that I need one extra line to make it work:
super.onStop();
It doesn't do anything (at least I can't see anything changed) and it's kind of useless, but without this line the app just keeps crashing.
Any help would be great, thanks.
It calls the onStop() method in the parent Activity class. When you look at the source code, you'll see that it does some internal housekeeping.
protected void onStop() {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
getApplication().dispatchActivityStopped(this);
mTranslucentCallback = null;
mCalled = true;
}
Your custom Activity extends the type Activity. The onStop() method is part of the super class activity. If you don't call super.onStop() the implementation on the Activity class is never called, and only your implementation is. This implies crashes since the onStop() method in the Activity classperforms clean ups that must be called.
The android reference on onStop() : http://developer.android.com/reference/android/app/Activity.html#onStop()
Your class is an activity subclass (extends Activity), witch mean that you must call super method of the activity mother class for more information about details of super.onstop() you can check the source code