Start new activity from notification in android - android

I want to start a Activity A from status bar notification, When the activity A is already in front then i want to finish that and fresh start activity A. How can i do this?

Review the documentation on creating Status Bar Notifications. This definitely covers starting and Activity from a Notification using an Intent and PendingIntent.
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
As for if the Activity is already running, finish it and start it freshly... I'm not sure that can be done easily, depending on what you really want. You may be able to do something with the launch mode activity parameter in the manifest:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
And then have your activity respond (with onNewIntent() most likely) and "reset" itself programmatically. Possibly with something like this:
Android restart my activity

You mean re-start Activity A? While the most common approach is just to re-launch a new Intent with your same class I think it uses way too memory. I'd rather create an "init" method which should be called from the onCreate AND when you want to re-launch your activity. Example:
public void onCreate(Bundle si){
// Call super and set your layout...
init();
}
/**
* This method should be called whenever you want to restart your activity. The
* biggest advantage is you already have your layout (setContentView() method)
*/
private void relaunchActivityA(){
// Clean or save anything you need to clean or save
init();
}
private void init(){
// Init your variables, threads, and so on
}
If you wrote 'finish that and fresh start Activity A' instead of 'Activity B', then right after your startActivity() -on activity A- call 'finish'. Example:
// This is inside Activity A
Intent i = new Intent(this, ActivityB.class);
startActivity();
finish(); // This will be called right after 'Activity B' finishes

Related

startActivity causes onCreate()

I have application and when I navigate back using Intent and startActivity(), views are null, onCreate() is called and activities are re-initialized. Why is that and how to bypass it?
I navigate back to activity like that:
#Override
public void onBackPressed() {
if (this.getClass() == XXX.class) {
Intent i = new Intent(this, YYY.class);
startActivity(i); //<-- activity restarts
return;
}
}
super.onBackPressed();
}
I use ActionbarSherlock, so I have activity with ActionBar initialization and every single activity just extends it. The way I navigate back to activity is described in this activity.
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
initUIComponents();
setListeners();
resetProgress();
}
and initUI() initializes UI.
EDIT
What I mean, how can I go back to previously created activity (not the one that is called via onBackPressed) and not recreate it? I use startActivity(), but apparently it recreates the whole thing
If you want that when you press back, you want to show the previous screen, then you don't have to do it in your code. Android Runtime internally maintains the stack, and will take care of showing the last-shown-activity when you press back. No need to handle it via onBackPressed()
However, if you want something other than this default action, that is when you should use onBackPressed(). Else, just let Android handle it.
So, in your application, if Activity 1 calls Activity 2, and user presses back, then the default action would be to show Activity 1 again. Don't override the onBackPressed() method
Edit:
For a custom flow of activities, you'll have to build the logic yourself. You need to override onRestart() in Activity 1, and onStop() in Activity 3. That way, onCreate won't be called again. By your logic, I mean, flags to keep track of which activity you're in, checking those flags, and calling the desired activity from there.
Edit 2:
This previous SO question, answers what you need:
Android Activity management , which suggests setting the flag FLAG_ACTIVITY_REORDER_TO_FRONT on the intent, and then calling startActivity()
Check out Android activity stack management using Intent flags for other stack reordering options: Stack management

One instance of an activity being started with an intent

I have an activity that is started from another activity via a button and which gets passed an intent when started.
I now figured out, that the activity is then created several times, since it is started with an intent, and will call onCreate() every time I hit the button mentioned above. In the documentation I read it says "Every time there's a new intent for a "standard" activity, a new instance of the class is created to respond to that intent", so that I assume that I then have several instances of that activity somewhere on the stack. This is what I would like to avoid, since the activity has some static variables which are referenced from other activites and I want to make sure that the value of this variable is deterministic!
What I want is to get rid of all existing instances and create a new instance of the activity when I press the button, or make sure to have just one activity and create it with the new intent everytime.
I tried to achieve this by setting android:launchMode="singleTop" for the activity and implementing onNewIntent(intent). This should make sure that I only have one instance of the activity in this scenario and I get the new intent. However if I do it like this, I basically have to copy my whole onCreate() method, but just using the new intent, which feels wrong.
public void onNewIntent(Intent intent) {
myAdapter= null;
serverThread.stopThread();
serverThread = null;
this.onCreate(...); // <- somehow do all this with new intent
}
Do you have a good idea how to solve this situation easily?
You can move all that code from onCreate() to onStart() to make sure it's called every time that activity is being opened
Try setting the flag FLAG_ACTIVITY_REORDER_TO_FRONT
It will ensure that if an activity exists in the stack, it will bring that activity to the front.
Intent intent = new Intent(getApplicationContext(),
YourCustomActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
In the above situation actually onDestroy() is called when the user is navigating back to the previous activity with the button creating the intent and starting the activity. I.e. actually in this case there is already only one instance of the activity and the previous one is being destroyed.
For this reason onNewIntent() is not called in the above setup. If you want to e.g. stop a server, as indicated above in the question by a piece of code, so that you can start a new one when pressing the button again, you hence can actually simply do this:
#Override
public void onDestroy() {
serverThread.stopThread();
super.onDestroy();
}
Hope that helps others :)

Apply check on application start to choose between different activities?

I have two activities but I want to apply a condition on application start, if one is true then start first activity else start second activity. Right now I am doing it by starting a third activity displaying some welcome stuff and examining the value in background..then appropriate activity gets called. I think there must be some standard way to do this. Peace.
Since you need to specify starting activity in manifest, you can always start first activity, check your condition on onCreate() and if you need to start second one - start second one and call finish() for the first activity.
Otherwise usually people use splash activity to check all the conditions at the startup (which is your current solution).
There's no need for a third Activity to check the condition.
If you simply have your MAIN/LAUNCHER Activity check on the condition as the very first thing it does in onCreate(...) (but after calling super.onCreate(...)), it can either continue or call startActivity(...) for the other Activity and immediately call finish() to self-terminate.
That way the first Activity will never be seen if the condition dictates the second Activity should be started.
Example..
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Don't even set the content view at this point
// we want to be invisible for the moment
// Pseudo-code check for condition
if (!runMe) {
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
finish();
}
else {
// Continue as normal
setContentView(R.layout.main);
...
}
}
}

How do I avoid onCreate() being called when starting an Activity?

I want to reload an activity from stack.
I use startActivity() to start new activities. When I'm on Activity D I want to reload Activity A and not start a new Intent. I can't use startActivity() when calling A from D because it will fire onCreate() which starts a thread to fetch some data.
EDIT: Updated the stack.
If I use FLAG_ACTIVITY_REORDER_TO_FRONT it calls the onCreate() method again.
Following is my scenario.
Login Activity ̣→ Activity A → Activity B → Activity C → Activity D → Activity A
How do I avoid onCreate() being called?
You have to take a totally different approach. It doesn't matter if you start your Activity with startActivity() or startActivityForResult() because onCreate(), onStart() and onResume() will be called when you start an Activity.
Now if you have a method in your Activity class that starts another thread to do some work then you have to work with flags. If your Activity requires to automatically start the thread on first execution then you have to wrap it around an if clause to check for a flag you set when it is first run.
The idea is to have your Activity set a boolean to true in either your Application instance or SharedPreferences when the thread is first executed. When you come back to that Activity and don't want that thread to be run automatically due to onCreate() being called then you have to wrap your calling code around some if clause like in the example below.
Here is an example.
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
// Other stuff
if (!YourApplicationInstance.wasCalled) {
// Run your thread or do something else you want to do only once.
// Set the wasCalled flag to true to not run this code again
// if onCreate() is called a second time.
YourApplicationInstance.wasCalled = true;
}
}
You'll have to read Using Application context everywhere? to understand how to implement my pseudo class YourApplicationInstance.
The following is not true. startActivityForResult() and startActivity() only differ in the return target of the called Activity
try using startActivityForResult() rather than
startActivity(). I believe this does not completely end the activity and start it again. I
would recommend using this link in order to further read on how to implement such a method.
So point 2 of #Kgrover does not hold too.
The Intent flag http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT does exactly this.
Keep an eye out on the Intent flags whenever you have requirements centered around Activity transitions. The system has excellent support.
This is equivalent to Sam Quest's solution, the only difference being that if you set the launchMode, the stack-behavior of your Activity is hardcoded i.e. your Activity A is always in the singleTask mode.
there is tag called launchMode for activity in the manifest. checkout this link. and this will not call onCreate but it will call onNewIntent, where you can reinitialize you stuff.
1) Although I am not sure, you could try using startActivityForResult() rather than startActivity(). I believe this does not completely end the activity and start it again. I would recommend using this link in order to further read on how to implement such a method.
2) Alternatively, when you go from activity D -> A, continue to use startActivity(), but pass in a dummy extra. Then in activity A, use an if statement:
if(!(this.getIntent().hasExtra("dummyStringExtra")) {
//fire the data thread here
}
Cheers. I hope this helps.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
This will kill the child activity. So parent activity is not recreated

Android: Different start activity depending on user preference

My application starts with a welcome screen Activity, but that screen has an option to skip that screen altogether in future launches.
What's the proper Android way to do this? Initially, I just automatically detected the skipWelcome preference and switched to the 2nd activity from Welcome. But this had the effect of allowing the user to hit the back button to the welcome screen we promised never to show again.
Right now, in the Welcome activity, I read the preference and call finish() on the current activity:
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean skipWelcome = preferences.getBoolean("skipWelcome", false);
if (skipWelcome) {
this.finish();
}
And then I implement onDestroy to move on to the next Activity:
#Override
public void onDestroy() {
super.onDestroy();
startActivity(new Intent(Welcome.this, StartFoo.class));
}
But this makes for some weird visual transitions. I'm starting to think that I need a base Activity that pops open Welcome only if proper, and then goes to StartFoo.
I can't comment on Mayra's answer or I would (not enough rep), but that's the correct approach.
Hidden in the Android documentation is this important phrase for Activity.startActivityForResult(),
"As a special case, if you call
startActivityForResult() with a
requestCode >= 0 during the initial
onCreate(Bundle
savedInstanceState)/onResume() of your
activity, then your window will not be
displayed until a result is returned
back from the started activity. This
is to avoid visible flickering when
redirecting to another activity."
Another important note is that this call does not block and execution continues, so you need to stop execution of the onCreate by returning
if (skipWelcome) {
// Create intent
// Launch intent with startActivityForResult()
return;
}
The final piece is to call finish immediately in the welcome activity's onActivityResult as Mayra says.
There are a few solutions to this.
Did you try just launching the activity and finishing? I vauguely remember that working, but I could be wrong.
More correctly, in if(skipWelcome) you can start the new activity for result, then when onActivityResult is called, immidiately finish the welcome activity.
Or, you can have your launcher activity not have a view (don't set content), and launch either the welcome activity or StartFoo.

Categories

Resources