Android: Recovery Activity When I press Home - android

i have the following question.
I have an activity which is showing a progress bar while a service is downloading data from an API.
I want that when i press Home and relaunch my program the activity and which is not the first activity called but it is in the stack was recovered in order to continue showing the progress.
I have read about the cycle of life of activities but i don't find a clear solution.
The same thing happens If i am in the activity which is showing the progress bar and i press a button that takes me to another activity, when i go back, can i recover the old activity instead launch one new?
Anyone helps me?
Thanks

The problem is that pressing the home button will erase the whole activity stack. That means there is no possibility to go back to the activity it even is not certain that the activity still exists.
If this a progress that is interesting for the user that it is still running you could display a notification bar icon until the progress is finished. I think you can specify a special intent for clicking on the notification bar and filter this intent with your activity. That way you would go back to the activity. But you still face the problem that the activity is saved and has no reference to the background thread doing the work.
If your Activity has left the stack its finish method is called. You shouldn't try to reuse this activity later on. The best way is to think of a way that the whole state of the activity can be saved and restored later on. To restore a reference to the background thread doing the work you could subclass the application class and save a reference to the running task in your subclass.

Related

Minimize and restore activity / Fragement

I have a Voip calling app using kotlin and when call received, it will open an activity called IncomingView. So far so good. But I want to Minimize the activity on back button and can navigate to other pages and shows a green bar or indicator at the top bar (similar to WhatApp/Telegram) that indicates that the call is going on and when i tap there, it should bring back my "IncomingView". (I know and already implemented that i can create a notification and tap on it brings back my activity)
Any Ideas?
But I want to Minimize the activity on back button
you cant minimize Activity, you can close it. You can "minimize" a fragment in a activity by removing it and later on adding it back - this way its state will not be lost.
If you need to minimize an activity, then I suggest to save its state, then minimize it (call finish() on it), then when you need it back then open it again and it should read its old state and recreate in a same way as it was previously.

how did facebook messenger switch between activities

how can facebook hide current activity and show another activity here in chathead
when choose another one to talk with him its hide current popup floating activity and show the other one activity
i don't think its start new activity because i write some message on current activity in the editText without send it and when choose another one to talk with him its hide current activity and when i back open it the text i write it it's still there thats mean it's dosen't start new activity when switch to other conversation
iam try to make something like facebook chathead but i have problem i switch between floating activities
They are using Fragments and Floating Action Buttons.
Also just for your information, unless you specifically call finish() on an activity, it will only get stopped when the new activity is started. When you return, it will go back to onResume() state.
Please look into activity life cycle and Fragment Life cycle to know about these things more clearly. The faster you have a clear idea about these, the easier it will be in the long run
https://developer.android.com/training/basics/activity-lifecycle/index.html
https://developer.android.com/guide/components/fragments.html
NOTE: When it is on the home screen, It is an window running on top of the background service with a floating window , while inside the app it is a Fragment.
see tutorial: http://androidsrc.net/facebook-chat-like-floating-chat-heads/

Activity not closing properly

I am calling one activity on click of status bar notification which is having a Complete button. on click of btn. i have folllowing code -
public void completeTask(){
taskDBAdapter.deleteReminder(rowId);
taskDBAdapter.close();
Intent intent = new Intent(this, TaskManagerActivity.class);
startActivity(intent);
finish();
}
whhen i click complete btn new activity (TaskManagerActivity) gets opened properly.But if i reopen my application it still tries to open this activity and not my default landing activity. Any help on this.??
EDIT -
I have tried repositioning my finish() statement . Still its not working.
EDIT 1.1 -
Ok I will provide some details here. Assume my app has two activities
Main Activity
Notification Activity
My app create some notification to display on Status bar. So as soon as i click on status bar Notification actvty will open. Now there is a button called Complete on click of which the code given will fire and main activity (in the code TaskManagerActivity.class) will open. But after I press back button in my app and again reopen it , it opens the notification activity when it should have fired the main activity (as it is launching activity).
Thanks,
Ray
That's the default way android functions. If you press the home button and then open your app again, it will restore the apps previous state (unless it has killed the apps processes and activities due to memory constraints). So you are not actually restarting your app but only restoring it.
If you wanna quit the app, then press the back button. Now when you re-open the app, the original activity will be launched.
Do not modify this behavior. It is the default system behavior and users expect it to work this way. Your app is fine :-)
- First of all the behavior which you are experiencing is the way Android is made to function, moreover when a user gets a call while this app is open, after finishing the app he will definitely want to get back to the state where he left the application.
Still if you want it that way, here it is.....
- Make sure your application has only single instance of it running, by using android:launchMode="singleTask", or android:launchMode="singleInstance"
- Then finish() your Activity at onPause().
#Override
void onPause()
{
super.onPause();
finish();
}

how do i close a specefic activity and get to previous activity Programatically?

Here i have a few activities that consist different menus in my app..
The problem is that i want to add a are you sure popup box to exit the current menu and return back but calling finish() method on the click event of yes button of popup box causes all activities to terminate and app exits...
I want to make a way to terminate only the foreground activity and
return to last activity programatically (i.e without using back key)
Can u post some source code regarding how you start you new activities? Are you starting multiple activities at all? finish() method only finishes the current activity and not the entire stack of activities, thus the system automatically brings to front the previous activity from the stack. I can't understand your question please provide some further details.

Android; How can I detect whether a Parent activity is still alive in Activity stack and then receate it

I have activities in sequence as Activity A calling Activity B,When I am on Activity B I press Home button and start another Application (for example Map). If i stay on this second application for a long time say 5-10 minutes and then press Home Button again and choose to Start my application again, then Activity B is started again and works fine. But when i Press Back key - I return to my Activity A again (which is also correct) but it shows a Blank Screen. Ideally in correct version it should show me Acitivty A with the XML data is ListView form.
Alternatively, in the above description, when the other Map Application is started and if the user stay on it only for 1-2 minutes then the above problem doesnt not occur.
Can anyone suggest a solution on the same.
Is it that i need to check in Activity B whether Activity A is still there in Activity Stack (How do i do the same) and If its not in my Activity stack then re-create it.
I tried doing alwaysRetainTaskstate in my Android manifest file for Activity A. But it doesnt work at all
You don't have to do any of that, Android takes care of the technicalities for you - it will recreate your Activity A.
You just need to remember to save A's state when B is opened (take a look at Activity.onSaveInstanceState). When A is restarted, your saved state will be passed to onCreate.
You should really read about Activity Lifecycle
This should help.

Categories

Resources