I have activities that are create and launched from menu options. However Ive noticed that this may mean that sometimes there are two or more copies of the same activity. So Im wondering if there's a way to see if another activity is already instantiated and then have the application switch to it or create a new one if its not instantiated.
You can control some aspects of this with android:launchMode on the activity.
Programmatically try following:
Intent intent = new Intent(contextActivity, NextActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
contextActivity.startActivity(intent);
You can specify information regarding that in the android manifest. See activity element documentation. I believe that launchmode might control what you are after.
Related
Can i call second activity from current activity without use intent? and why use intent, why not call second activity direct from the first activity?.
What does intent do in android?
Because to start any new Activity Android must have to go through the life cycle of an Activity. So it is necessary to use intent.
http://developer.android.com/reference/android/content/Intent.html
you can't ,see this link android is designed in this way to launch another activity
Intent says to Android you need open a new activity. Always when you need open a new Activity, you need "alert" the S.O before. Because of this we use Intent.
Activities doesn't work like a simple class, when we just instantiate.
This link will help you
http://www.vogella.com/tutorials/AndroidIntent/article.html
I have the below code:
Intent myintent = new Intent (ScreenOne.this, ScreenTwo.class);
startActivity(myintent);
ScreenTwo consists of a list of phone numbers.
Both the screens extend BaseScreen.
What I need to do is click on a phone number and make a call from within my app.
I have the code right, only problem is the call screen is behind ScreenTwo.
I am guessing it is something to the with the context that I am passing in the Intent.
I have used
ScreenOne.this,
this,
getBaseContext(),
getApplicationContext(),
getApplication and
getParent(),
all to no avail.
ScreenTwo itself is supposed to be an Activity since it extends Activity. You should create some more new activities (and register them in AndroidManifest.xml too) and then call them as you are doing in your first approach.
There was no light shed on this, and all the contexts don't seem to work in bringing the screen to the top, I guess I'll have to find another workaround.
Is it better to start a new activity or just set a new content view in Android?
I usually start a new activity when I have to change the whole environment, but IMHO I think it's too onerous when I need to keep the other activity alive in the background.
On the other hand, I could have a single activity and just change the layout when I have to set a new environment.
Which is the better way and why?
Search for 'Pattern one activity, multiple views. Advantages and disadvantages' on stack-overflow and look for the answer by #CommonsWare
If you have several activities onPause() is there a way to finish a specific activity?
edit: so for example, imagine on start activity 1 is called. Then activity 1 uses an intent to go to activity 2. then an update is made to the database and calls activity 1_new again so that it displays the updated database. At that point i want to get rid of the old activity 1.
It depends on what you want to do. You'll need to look at the AndroidManifest.xml spec for activity calls stacks.
Specifically android:launchMode
<activity android:launchMode="singleTop">
Careful though, launchModes are very tricky and can get you into trouble since it also depends on how the activity is launched from the Intent itself.
singleTop will essentially keep only 1 instance of that activity in the stack.
From the Docs:
If an instance of the activity already
exists at the top of the target task,
the system routes the intent to that
instance through a call to its
onNewIntent() method, rather than
creating a new instance of the
activity.
What I ended up doing here was calling startActivityForResult in the first activity. That way I was able to redisplay updated information from the second activity.
Is it possible to create implicit intent to call our own activity? If possible is it useful or better option is Explicit Intent?
please explain your situation more...
If a single Activity is there and you want to call the same activity again from this to have any refresh sort of thing...
Then its not a good idea...
All the views can be updated without calling the same activity again.
And if new view is to be generated then use another activity