Resume Activity by service in Android - android

I want to develop a program which reminds word (english-turkish). The things what I have to do below and please correct me if I'm wrong or bad way I'm using.
Create and Main.java class as an activity which includes a textview to show english word
Create another activity which shows Preferences to setup interval time to remind new words
Write some code under the Save button click(inside the Prefs.java class) to save settings to SharedPreferences
Inflate menu inside Main activity to show Preferences Activity
Create a service with MyService name.
Get interval from SharedPreferences inside the OnCreate method of MyService.
Inside the OnStart method Run a Timer according to interval and continousley connect to web service to get a new word.
Periodically bring to front Main activity(don't want to create every time from the begining, just want to resume activity) and show new word.
When pressed New Word use the service's function to connect and retrieve new word and show in TextView in Main activity
When pressed Ok set Activity to Pause mode and show Home Screen
I have some difficulties to Resume Main activity and passing new word.
Do you know a way to bring front Main activity periodically while it is in resume state?

Try to make an intent in the onResume() call with this flag: FLAG_ACTIVITY_REORDER_TO_FRONT which causes the launched activity to be brought to the front..
For more information click here.

There is no difference between starting activity and bringing them to foreground - read about activity lifecycle.
Service is ineffective if you want to use it as simple timer. Much better approach is to use AlarmManager and scheduling next activity start. Here you have an example.
Then just override Activity.onStart() method, to fill all fields you want.

Related

Change Android's activity on background

I have a app that uses webscockets, and when the app goes to backgroud i need after a x amount of seconds to disconnect the websckets and change the current activity to the lobby activity.
I have a singleton that extends Application and implements
ActivityLifecycleCallbacks, each activity is registered on registerActivityLifecycleCallbacks.
The problem is that when the app is in background and i call the lobby activity, the new activity calls all Activity Lifecycle Callbacks, like onActivityCreated, onActivityStarted, onActivityResumed and then onActivityStoped, and that behavior forces my to do a bunch of checks to see if the lobby activity is called on background or on foreground.
Is there a way to do this without that behavior, like to put the activity on hold ready to start on app resume?
And i prefer not to call the new activity when the app resumes it feels jerky, because you see the last open activity and then you see the change to the lobby activity.
Sorry for the long question.
You should use a Service to achieve that you want
Visit http://developer.android.com/guide/topics/fundamentals/services.html
Hope this will help :)

How can I return to a specific activity that has already been created previously?

My end goal is this:
MainActivity has a button that starts SubActivity (which is essentially a timer).
Each time the button is pressed a new SubActivity starts.
MainActivity also has a RecyclerView that displays a card for each SubActivity so that you may return to any timer that you have already started.
My problem is I don't know how to return to each Activity. I already did some research, and most people point towards onSaveInstanceState, but from my understanding that simply would recreate the activity by saving values and restoring them. I know my timer SubActivities still proceed in the background because even when I leave the SubActivity with the timer running, the alarm will go off later, but I am left unable to reopen that activity in the meanwhile.
So how do I access the running timer activity?
Thank you!
Not clear what you want to achieve really, but it looks like wrong app design. If you want timer to run regardless of activity then you should decouple timer from activity and make activity just display the timer. Your timers should be elsewhere and definitely its lifecycle should not depend of actvity lifecycle.

How to keep the activity state intact on back press in android?

I am new to Android . As per the Android Developers Doc making an activity launchmode singleTop it will keep that activity intact . But its not working for me .I have an Activity where i have a countdown timer , what i want is when i leave that Activity on back press and return to that Activity that countdown timer should still be running . How to do it ? Please Help
I believe you misunderstood a bit.
Launching an activity in singleTop does not mean that the activity is "intact", it means that if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent - a new instance won't be created. (This is opposite to launching activities in standard mode, which every time there's a new intent a new instance of the class is created to respond to that intent.)
As others suggested, you could bind to a Service and update the countdown time from there.
"Creating a Service will always keep that timer running , instead i want it to keep running only when application is alive", that means you want to continue the timer from where the user left.
Store the timer value in SharedPreferences onStop() and retrieve the same onRestart() and then finally continue updating

how to show android timer timings as alerts in different activities?

I am implementing a timer in my application which checks an alarm all the time.This timer values am checking in my home page and showing alerts telling how much time left..How can i show these messages when i am in different Activities ?
One way would be to add your timer check code to a Fragment with no UI and use that Fragment in your various activities.
If your dialog box doesn't interact with your activity then you can start a TimerTask in a subclass of the Application object that checks your timers as often as you need. Then when a timer needs to go off, you can start a new activity that simply displays the alert dialog. Since you're starting it from the Application (which subclasses Context), you need to set the NEW_TASK flag and theme the activity to be a "dialog". You could even make the whole activity the dialog instead of starting the dialog in onStart()

Android restart activity (with AsyncTask) on completion of another activity

I suppose the title is a bit confusing but here is what I'm trying to do:
I have a class called ManageClass which lists the entries of a database. I have written another private class within ManageClass which extends AsyncTask so that I can display a progress dialog while I'm getting the data from the database. Now when I click on an item I create a new Intent which takes me to my ViewItem class. I've added a button there so that the user can delete that particular entry that he/she is looking at. All of the above work fine.
Now I want after deleting that entry to kill the activity and go back to the previous one (the one displaying the list) but I want to refresh the listings.
My problem is that I cant use onResume() cause it will also be called when the activity is resumed after the AsyncTask finishes.
Could anyone help me with that? I'm really stuck... all ideas are welcome!!!
If I understand your app workflow you should use startActivityForResult instead of launching a new Activity via intent.
Look at here fore some example
Basically you can launch a new Activity and wait for a result via callback on the "opener" activity. so you can avoid to put your logic into onResume method

Categories

Resources