How to start new activity without killing current activity - android

I have 2 activities (audio chat activity & message chat activity). Currently both activities working absolutely fine but now I want to both activity should work simultaneously like a Whatsapp messenger.
In audio chat activity there is a button to go into message chat activity but once message chat activity starts the audio chat activity stop working.
Is there any way where I can switch between these two activities like a Whatsapp messenger.

You should change approach. When you start a new Activity B, Activity A is paused, so it can't work. For your purpose you should use a single Activity and show/hide elements (e.g.: messagebox and audiobox)

There is no way to guarantee that an activity that sits below another activity will never get killed. Android can do this to reclaim memory when needed. It's the way Android was designed to facilitate devices with limited resources.
Instead, if you want to let the user seamlessly switch between text and audio chat, you will need to maintain the UI between those two components in the same activity. The easiest way is to simply have two different sets of views in the view hierarchy for the activity and switch between them by changing their visibility.

I hope this will helpful to developer,may be fresher or senior,I have ActivityA and B,this is my ActivityB class
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intnt=new Intent(getApplicationContext(),ActivityA.class);
startActivity(intnt);
}
please use this step in ActivityB to move back your AcitivityA without killed
And solution is
#Override
public void onBackPressed() {
super.onBackPressed();
}
//I hide these below intent(2lines) in ActivityB,So it works thanks by Daemon....

Related

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/

How to finish a specific activity in android

I know you might think that this question have been asked a lot, but I've been looking into different cases, all cases they just want to close all activities when logged out. Here is my scenario:
1) Start the app with Splash Screen then I used finish()
//I set this activity as MAIN so it's first to open - that is why I cannot setFlags(FLAG_ACTIVITY_CLEAR_TOP) because it basically goes back to MAIN
2) Introduction Pager(4 sliding pages) with Login and Signup buttons
3) Login and signup buttons direct you to corresponding activities
// I didn't use finish in introduction pages because I want the user to have the ability to go back to introduction if for example pressed signup by mistake
4) after performing login/signup the user will be directed to the homepage(Here I used finish to kill login/signup activities because I won't need them anymore)
So all previous activities are now finished except for the introduction pager, when the user press back the app will finish homepage and go back to introduction, but I don't want that! I just want the app to quit, I don't want the introduction pager to be running on the background. How do I finish introduction pager when onCreate method is called in homepage?
I have another related question I didn't want to post another thread for it: how to get the previous activity? like I don't want to use the action bar to navigate, I created a button (<- Back) and when I press it I want to bring the previous activity, how do I get the previous activity?
First of all make your IntroductionActivity a single istance in the manifest like this: launchMode = "singleInstance"
There are many ways you can do this, the fastest, I guess, is to use a static field that refers to that activity. Add this in your IntroductionActivity:
public static Activity mActivity;
onCreate(){
this.mActivity = this;
}
Then when you reach the homepage you can do this:
onCreate(){
IntroductionActivity.mActivity.finish();
}
Use finish(); after you start the next activity
Example:
Intent itr=new Intent("com.example.splash.Second");
startActivity(itr);
finish();

Android: Removing all previous activities from the stack

This question has been answered numerous times it seems, but none of the answers solve the problem for me, so I thought I'd ask a new one.
My scenario is as follows:
I have added a navigation drawer to my app that holds list items for each section of the app. Tapping one of these launches the appropriate activity for that app section.
The way I wanted to do this was to have a main activity which had the drawer layout and then everything else would be fragments, but the app has a lot of activities already and the customer isn't willing to pay for the extra time that would be needed to convert these to fragments and get it all working. Therefore I'm keeping it as it is and have the nav drawer on each activity.
When the user clicks an item in the nav drawer, that section's activity is launched. If the user presses the back button on any of the activities that the drawer brings them to, I want the app to close (and go back to the Android app menu or home screen or whatever).
My problem is that pressing back will just pop the activity that the user was previously on when they clicked an item from the drawer. For example:
User is in Activity A.
User opens drawer and clicks list item.
Activity B is opened.
User presses the back button.
Activity A is shown.
I'm looking for a way to remove all previous activities from the stack, so that when a nav item is clicked, everything already in the stack is removed so that pressing back on the new activity will end the app because there won't be anything else to show.
The closest I've got is using the FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK flags on the intent that is launched on the nav list item click.
sectionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
parentActivity.startActivity(sectionIntent);
(where sectionIntent is an Intent for the new activity and parentActivity is the activity hosting the drawer).
This actually works, but an empty (black) screen is displayed for half a second or so between the old activity closing and the new one showing.
I've also considered using LocalBroadcastManager to tell all activities to close when a "top level" (as such) activity is exited, but to be honest that approach seems like total overkill to me and surely a simpler option must exist?!
Does anyone know a way either to prevent this delay (like for the previous activities to be removed AFTER the new one appears) or another way to remove the activities in the stack?
Thanks in advance, fellow devs :)
-- SOLUTION --
Thanks to one of #Zielony 's suggestions, I managed to get this working.
I made a base top level activity and made it send a local broadcast to exit the app.
public class AppSectionHomeActivity extends BaseActivity {
#Override
public void onBackPressed() {
sendFinishActivityBroadcast();
super.onBackPressed();
}
#Override
public void finish() {
sendFinishActivityBroadcast();
super.finish();
}
private void sendFinishActivityBroadcast() {
// Send a local broadcast to all other activities to tell them to close.
Log.d("AppSectionHomeActivity.sendFinishActivityBroadcast", "Sending broadcast");
Intent intent = new Intent();
intent.setAction(Constants.EXIT_APP);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
}
I then made a base activity for all other activities (the ones that weren't top level ones) so make them listen for the broadcast and then call finish() on themselves if they received it.
public class BaseActivity extends Activity {
// A broadcast receiver so that we can listen out for feed updated events.
private BroadcastReceiver exitAppReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
unregisterReceiver();
// When the activity receives the broadcast to finish up, then do so.
finish();
}
};
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(exitAppReceiver, new IntentFilter(Constants.EXIT_APP));
}
#Override
protected void onDestroy() {
unregisterReceiver();
super.onDestroy();
}
private void unregisterReceiver() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(exitAppReceiver);
}
}
This seems to work!
You have several choices:
use Broadcast to tell the stack to close itself and then add new Activity. It's quite good solution, but seems like you tried something similiar. Also, this shows best that you shouldn't attempt to close multiple activities. I don't know the thought process behind that.
use Process.kill() to kill your application when exiting most recent top-level Activity. It would look like exiting the app without the rest of the stack. It's not that bad either - Android will close unused Activities anyway, so it's not that resource demanding.
use Fragment stacks. I know your client wants you to do it quick and cheap, but fragment stacks are really good for this.
use Tasks to group Activities and close groups at once. Seems like it's not working for you and I don't really know why. This is the pre-fragment solution for your case.
Have you considered using fragments instead of activities? This allows a lot more flexibility when things will be shown/hidden/available etc.
Are you using a new nav drawer for each activity?
You can try like this. such as
sectionIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
parentActivity.startActivity(sectionIntent);
Best of luck!
In Manifest file, which ever activity you don't want in stack, in that activity make this changes
android:noHistory="true"
I hope this works for you

Close the whole stack of activities in Android without graphical issues

I am trying to close a whole stack of activities using the way described here:
How to kill an application with all its activities?
Namely, each activity starts the other one with startActivityForResult, and in onActivityResult calls finish() to close itself together with the activity it opened.
The problem is that the activities in the task still seem to repaint themselves at least once before they close, and this doesn't look good. After closing the topmost activity one sees all previously opened activities like in a very fast slideshow.
How can one avoid this graphical issue?
EDIT: I need that if the user presses HOME button and then presses the app's icon in launcher, he returns to the current state of the stack, not to the very first activity again. So, from what I understand, with such a requirement I can't finish() activities before starting next ones.
That's native behaviour, intended to aid in user Experience. When an Activity is started with startActivityForResult and then finishes, it will (on devices that allow fancy animations) automatically slide away. That helps people not get surprised by the screen suddenly changing.
You could try starting the Activities without startActivityForResult and handling the passing of data to and from Activities manually, then handle how/when Activities finish() and which Activity they pass back to. You might find you implement something where Activities actually pass forward to the appropriate Activity all the time, rather than back to an Activity on the stack.
Intent intent = new Intent();
intent.setClass(getApplicationContext(),
PhoneListCheckboxAES.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
If u give like this when u are starting the next activity then the graphical problems won't occur

How to make an activity stop, rather then be destroyed, from the BACK key?

Right now an activity gets destroyed when the BACK key is pressed. How can I make it just stop ( i.e. keep all the variables, etc. alive ), rather then be destroyed?
Thanks!
Why is it that you need to keep the variables alive? Given the established lifecycle of an Android application, I'm not sure that preventing the activity from being destroyed "just to keep the variables" makes sense.
Even if you stop the application without destroying it, there is always the chance that Android will kill it to free up memory. You will have to account for this in your code anyway, and so preventing the application from destroying doesn't save you from writing code.
Variables can be saved and restored relatively easily and quickly using SharedPreferences in your onPause() and onResume() methods. Unless you are storing a ton of data, preventing the application from destroying might not make much of a difference.
It sounds like you want to keep the variables in memory because you intend to return to this activity. Typically, you don't use the back button to navigate away from activities that you intend to come back to. Instead you would create an Intent and start a new activity. When you do this, Android places the current activity on the Back Stack calling onPause() and onStop(), which seems like exactly the sort of behavior you are looking for.
So if you still really want to prevent your activity from being destroyed (at least until Android decides it's using too much memory and kills it on it's own) you could always use Sagar's code and start a new activity in onBackPressed().
#Override
public void onBackPressed()
{
Intent intent = new Intent(this, Other.class);
startActivity(intent);
}
Just be certain that that is what you really want to do.
Simple one line
#Override
public void onBackPressed() {
mActivity.moveTaskToBack(true);
}
Pressing the BACK key triggers the onBackPressed callback method of Activity class. The default implementation of this callback calls the finish() method.
http://developer.android.com/reference/android/app/Activity.html#onBackPressed()
You can override this method to move the activity to background (mimick the action of pressing the HOME key.
eg:
#Override
public void onBackPressed() {
onKeyDown(KeyEvent.KEYCODE_HOME);
}
You could also instead consider moveTaskToBackground() mentioned here:
Override back button to act like home button
I have managed to work out exactly what you want: switch between 2 activities using Back button and keep them all not to be destroyed!
For example: you have 2 activities A & B. A will be started first, then A calls B. When B is loaded, user press Back button and switches back to activity A from B. From now B should not be destroyed and just goes to background, and when user starts activity B from A again, activity B will be brought to foreground, instead of being re-created again or created new instance! How to implement this:
1. Override onBackPressed() of activity B:
#Override
public void onBackPressed() {
Intent backIntent = new Intent(this, ActivityA.class);
backIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(backIntent);
}
2. On activity A, call activity B:
public void callActivityB() {
Intent toBintent = new Intent(this, ActivityB.class);
toBIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(toBintent);
}
remember to add flag: Intent.FLAG_ACTIVITY_REORDER_TO_FRONT when you call A&B.
This is similar to this question that was asked earlier.
Hope this helps!
N.S.
First of all, sorry for not answering the question, cause, I still have no optimal answer for it.
But, I really like when people start asking "what do you need this for". And, very rarely, the person who asked the question, really deserves this kind of question. I think not this time, but ok, this is not the issue...
Anyway, I will try to point out why some of us are convinced that
going from Activity A to Activity B(creating UI based on some data fetching) AND
going back from B to A(destroying all the created UI and/or fetched data in B) is sometimes a bad concept. Better solution would be to keep the stack as it is, so using something like finish() in Activity B, but keeping the Activity B in Pause state, so later when calling it again from Activity A - it just goes in onResume = nothing recreated in UI, no additional data fetching. The bonus of course is a fast and responsive UI, and the difference is really if you have a more complicated UI layout.
Just specify in the manifest for the activity as
android:persistent="true"
That should prevent your activity getting destroyed. To know more about this please refer to these below links which were answered by me
How to prevent call of onDestroy() after onPause()?
Prevent activity from being destroyed as long as possible
In the above posts I have explained in detail with a use case

Categories

Resources