I've read a few articles here (and other places) that describe how to dynamically choose which activity to show when launching an app. Below is my code:
AndroidManifest.xml
<activity android:name=".StartupActivity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
StartupActivity.java
public class StartupActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent;
if (RandomClass.getSomeStaticBoolean())
{
intent = new Intent(this, ActivityOften.class);
}
else
{
intent = new Intent(this, ActivityRare.class);
}
startActivity(intent);
finish();
}
}
Both ActivityOften and ActivityRare are declared in the manifest (without the launcher category of course) and extend ListActivity and Activity respectively. 99% of the time the 1st activity to get shown is ActivityOften based on RandomClass.getSomeStaticBoolean().
So launching my app from the icon for the 1st time I break inside the StartupActivity.onCreate. The choice is properly made. But then any subsequent attempts to launch the app (from a shortcut or the apps menu) show the ActivityOften again. No further breaks occur inside the StartupActivity class. Despite the fact that I know that RandomClass.getSomeStaticBoolean() has changed value and that ActivityRare should appear, the 1st activity keeps popping up.
Any ideas?
Thanks, Merci, Gracias, Danke, Grazie!
Sean
It is happening because your application activity is loaded from the history stack.
Set android:noHistory=true in the manifest for both ActivityOften and ActivityRare. That should solve your problem.
Just as a suggestion, you could just have one activity instead of three by choosing the content View dynamically. i.e.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (RandomClass.getSomeStaticBoolean())
{
setContentView(R.layout.Often);
// Set up often ....
}
else
{
setContentView(R.layout.Rare);
// Set up rare ....
}
}
This would mean that you would have to write setup code both views in on activity, which can get a bit messy.
Related
I have an application with three activities.
MainActivity which looks like that:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button b = new Button(this);
b.setText("click me to go to child activity");
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ChildActivity.class));
}
});
setContentView(b);
}
}
ChildActivity which looks like that:
public class ChildActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TextView(this) {{
setText("I'm the child activity");
}});
}
}
And OtherActivity which looks like that:
public class OtherActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TextView(this) {{
setText("I'm other activity");
}});
}
}
In the manifest I have such declaration:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name="pl.psobolewski.test.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="pl.psobolewski.test.ChildActivity" />
<activity android:name="pl.psobolewski.test.OtherActivity" />
</application>
Now when I start the application, it starts with MainActivity, from there I can go to ChildActivity, but there is no way to go to OtherActivity.
Then in the manifest I change this line:
<activity android:name="pl.psobolewski.test.ChildActivity" />
to:
<activity android:name="pl.psobolewski.test.ChildActivity" android:parentActivityName="pl.psobolewski.test.OtherActivity" />
Now I start again this application on my phone, which has Android API 16. It starts with MainActivity, there I can press the button and move to ChildActivity. Now the ChildActivity looks a little bit different than before: the logo on ActionBar has a little arrow-like icon (documentation calls it "a left-facing caret") which means it can be used to move up. But when I press it I don't go to OtherActivity - even though it is declared as the parent of ChildActivity - but to the MainActivity.
I find it contrary with the Android documentation which says:
http://developer.android.com/guide/topics/manifest/activity-element.html
"android:parentActivityName
The system reads this attribute to determine which activity should be started when the use presses the Up button in the action bar. The system can also use this information to synthesize a back stack of activities with TaskStackBuilder."
I also thought that adding android:parentActivityName attribute without calling setDisplayHomeAsUpEnabled would not turn the application logo into the up button - the documentation at http://developer.android.com/training/implementing-navigation/ancestral.html suggests so.
My question is: why the "up" button moves me to the MainActivity and not to the OtherActivity?
The Action Bar up navigation handler has been implemented in such a way that if the parent of current activity has no parent, then an Intent is created with ACTION_MAIN & CATEGORY_LAUNCHER to start the activity. This results in MainActivity being launched.
Have a look at definition of getParentActivityIntent() in Activity.java
To overcome this, in your ChildActivity.java override below 2 methods of Activity.
#Override
public boolean shouldUpRecreateTask(Intent intent) {
return true; // This creates a new task stack
}
#Override
public Intent getParentActivityIntent() {
Intent intent = new Intent(this, OtherActivity.class);
return intent;
}
If you don't want to override getParentActivityIntent, then you need to define a parent activity for OtherActivity in AndroidManifest.xml file, to overcome the earlier mentioned reason.
If you don't override shouldUpRecreateTask, since OtherActivity does not appear in history stack, it will remove all activities until the root activity of the task is reached, resulting in 'in-app home' behavior.
I'm a totally noob on Android, is there a way to execute an app without a layout? The process would be like: Click app icon -> run some code (Without prompting any window) -> display toast.
The trick is to open a transparent activity, show the toast and finish the activity, which makes it look like only the toast is displayed because the activity which opened was transparent.
To do this you can do.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, messageToBeDisplayed, Toast.LENGTH_SHORT).show();
// finish the activity as soon as it opened.
this.finish();
}
}
Also you need to give a transparent theme to your activity by specifying it in AndroidManifest.xml, For which you can use NoDisplayeTheme provided by Android like this.
<activity android:name="TransparentActivity"
android:theme="#android:style/Theme.NoDisplay">
</activity>
Yes you can by adding:
android:theme="#android:style/Theme.NoDisplay"
in your activity in Android manifest.
Check this answer for more details.
Use this:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
this.finish();
}
}
and in manifest file add: android:theme="#android:style/Theme.NoDisplay"
So My application Consists of first activity which shows some text, and on action bar there is a File menu, in which I put My location option.
I call another activity in mainActivity with onOptionItemSelected as follows:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_photo:
openPhoto();
return true;
case R.id.action_video:
openVideo();
return true;
case R.id.action_map:
Intent intent = new Intent(this, GPSTracker.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
in manifest i declare the second activity as follows:
<activity
android:name="com.example.locateme.GPSTracker"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and in GPSTracker.java i write this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gpstracker);
}
also there is my code for finding the location.
I am running the app, bu when i press My Location option the app crashes.
Here are the logcat errors after removing intent for GPS activity
The full code of the app is here, in case there may be something i missed.
Do i call the second activity in the wrong way?
java.lang.InstantiationException: can't instantiate class com.example.locateme.GPSTracker; no empty constructor
is quite obvious error.
Somewhere in your GPSTracker class you have a definition like
public GPSTracker(SomeClass referenceName) {
//...
}
This block of code should be removed or replaced with constructor without params. The first option if prefered: use onCreate as your constructor.
First, remove the intent-filter for Your GPS Activity in Your Manifest.xml, here You had set both activities (Main and GPS) as a launcher. Set only one Activity as LAUNCHER and MAIN. And then it will be good to see LogCat Output to know why it crashs
The application(target API level must be 7th) has FragmentActivity which analyzes at onCreate the fragment key passed as an extra.
Now what is needed is to reorder to front the activity that is already created with the given fragment key.
Let's say the FragmentActivity with different fragment keys are FA1, FA2 and FA3 - each is the same activity Class instance with different fragments.
Now in the stack FA1 > FA2 > FA3 i want to use the intent rather than the back button to get to FA2, by default that gives:
FA1 > FA2 > FA3 > new FA2.
I'd like to get either FA1 > FA3 > FA2 as the FA3 might have some pending operations, FA1 > FA2 is not as good but definitely better than default.
If there were several activities I'd use the FLAG_ACTIVITY_REORDER_TO_FRONT flag for intents, but that does not work for this case.
FA1, FA2, FA3, etc. are all the instances of the same class MyFA, that's why I'm not able to use the intent flag and the FragmentManager seems to be out of help until there's a standard global fragments cache.
Milestone (currently working and to be improved) solution One thing I've learned today is activity-alias which allowed to make several aliases for the same activity with the different Intent extras used as id's. Now with the REORDER_TO_FRONT flag it works as I wanted.
Solution feedback The solution has no low-level operations, I like a lot more than digging at the tasks or back-stacks. Now the drawback is that each of such activities needs a separate alias with the hardcoded path, I don't really like it.
Requirements (bounty is here) Whoever comes with a decent optimization takes 150 300 cookies. Not bad ? Any other solid solution is also highly appreciated.
Currently I have like 10 aliases at application manifest, e.g.
<activity
android:name=".activity.FragmentActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="com.company.name.intent.FragmentActivity" />
</intent-filter>
</activity>
<activity-alias
android:name="com.company.name.intent.FragmentActivity.FragmentedOne"
android:targetActivity=".activity.FragmentActivity" >
<intent-filter>
<action android:name="com.company.name.intent.FragmentActivity.FragmentedOne" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="fragment_key_extra"
android:value="FragmentOne" />
</activity-alias>
<activity-alias
android:name="com.company.name.intent.FragmentActivity.FragmentedTwo"
android:targetActivity=".activity.FragmentActivity" >
<intent-filter>
<action android:name="com.company.name.intent.FragmentActivity.FragmentedTwo" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="fragment_key_extra"
android:value="FragmentTwo" />
</activity-alias>
And then the activities are reordered with
Intent intent = new Intent(
"com.company.name.intent.FragmentActivity.FragmentedOne");
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
This answer is worth 300 cookies :-D Mmmmm
Extend Application so we can create our own back stack list with a global scope.
import android.app.Application;
import android.content.Intent;
class MyApp extends Application {
//This is our very own back stack.
private ArrayList<Intent> backStack = new ArrayList<Intent>();
public void reorderBackStack(){
//Do what you want to the order of your backstack
//You can read the value of your intent extras from here.
}
public void addIntent(Intent i){ // this gets called from our activities onCreate
backStack.add(i);
}
public void goBack(){
if(backStack.size() >= 2){ // can go back
backStack.remove(backStack.size() - 1); // drop the last item in stack if you want. This is how Android does it. (no forward capability)
this.startActivity(backStack.get(backStack.size() - 1));
}
}
}
Add a reference to our custom back stack in our activity.
public class MainActivity extends Activity {
MyApp myapp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myapp = ((MyApp)getApplicationContext());
myapp.addIntent(this.getIntent()); //add the intent for this instance to backStack.
myapp.reorderBackStack(); // call this anytime we need to reorder the back stack.
}
#Override
public void onNewIntent(Intent newIntent){
this.setIntent(newIntent);
myapp.addIntent(this.getIntent());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
// We won't be using the back stack we will be using our own list of intents as a back stack so we need to override the back button.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
myapp.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
Override the back button so we can use our own back stack instead of Android's.
Edit
I put together a proof of concept.
Pros
No Aliases
Completely dynamic and reusable
Low level control
Easy to use (Extend CustomBackStackActivity instead of FragmentActivity or Activity)
Cons
Not an out-of-the-box solution. Will require tweaking and debugging.
The .apk is Here on my google drive.
The complete project folder is here.
Zipped downloadable project folder is here.
Please don't complain about my code. I know I used strings where I should have used constants and I duplicated code instead of seperating it and my spacing isn't perfect and I used excessive loops and objects. I haven't sorted out the savedInstanceState. Again, this is just a proof of concept. It works and I thought it might help someone.
You can look into using the functionality provided by the FragmentManager. It has functions such as popBackStack.
You can also use FragmentTransaction to add fragments to the backstack. You can use addToBackStack to accomplish this.
So with these two classes you should be able to reorder your fragments in the backstack as necessary.
Here is the solution:
1. In your FragmentActivity implement a Interface for the callback from Fragment to Activity.
2. When you start any Fragment put it in back stack with the argument or tag (addToBackStack(String arg0))with the help of which you can pop up with that tag.
3. When you need to reorder the fragments then call the method of the interface which you implemented with proper arguments as per requirement and then use popBackStack(String arg1, String arg2) to get the fragment with the tag you put it in the backstack.
Based on your idea of using activity-alias to solve this issue, I wrote a Historian class that will do the following:
Scan the Activity list in your package for aliases to your specific Activity.
Set up a lookup table that maps each alias to an Intent.
Provide a startActivity() and activityDestroyed() methods that will do some bookkeeping so the lookup table can be used to dynamically assign an alias to a running Activity based on the Intent.
Here's an example on how to use it:
in AndroidManifest.xml
<activity android:name=".MyFragmentActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity-alias android:name=".Alias0" android:targetActivity=".MyFragmentActivity" />
<activity-alias android:name=".Alias1" android:targetActivity=".MyFragmentActivity" />
<activity-alias android:name=".Alias2" android:targetActivity=".MyFragmentActivity" />
<activity-alias android:name=".Alias3" android:targetActivity=".MyFragmentActivity" />
<activity-alias android:name=".Alias4" android:targetActivity=".MyFragmentActivity" />
within your Activity class
public class MyFragmentActivity extends FragmentActivity
implements Historian.Host {
private Historian<MyFragmentActivity> mHistorian;
// ...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHistorian = new Historian<MyFragmentActivity>(this);
// ...
}
#Override
protected void onDestroy() {
super.onDestroy();
mHistorian.activityDestroyed(this);
}
#Override
public boolean matchIntent(Intent intent0, Intent intent1) {
if (intent0 == null || intent1 == null) return false;
final String title0 = intent0.getStringExtra("title");
final String title1 = intent1.getStringExtra("title");
return title0.equals(title1);
}
// ...
}
Instead of starting a new instance of your activity like this:
Intent intent = new Intent(this, MyFragmentActivity.class);
intent.putExtra("title", newActivityTitle);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
You do this:
Intent intent = new Intent(this, MyFragmentActivity.class);
intent.putExtra("title", newActivityTitle);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mHistorian.startActivity(this, intent);
So you still need to add a few activity-alias into your manifest manually (to be used as a pool) and implement the matchIntent() method in your Activity (to help detect whether two Intents are equal to you) but the rest is handled dynamically by the Historian class.
I haven't tested the code exhaustively, but it seems to work fine on some simple tests that I did. The idea is actually very similar to my answer on the other question (just need to use FLAG_ACTIVITY_CLEAR_TOP instead of FLAG_ACTIVITY_REORDER_TO_FRONT there) but using the activity-alias instead of the inner child classes make it much cleaner :)
I have answered something similar, but the solution is not the best as FA1 > FA2 > FA3 will take you to FA1 > FA2 rather than FA1 > FA3 > FA2.
My answer was for question : How to tag Activity
Anyway, I edited the code a bit to use a "tag" which will be a string, but I think you can use the main solutions that uses integer indices. In summary, this should allow you to fallBackToActivity with a certain TAG. I am not sure how will each Activity decide what will its tag be, but again, in my previous answer it was a simple matter of an incremental integer.
package sherif.android.stack.overflow;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class SuperActivity extends FragmentActivity {
private static String EXTRA_INDEX = "SUPER_INDEX";
private static int RESULT_FALLBACK = 0x123456;
private String tag; //this is your tag
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getIntent()!=null) {
tag = getIntent().getStringExtra(EXTRA_INDEX, "default_tag");
}
}
protected final String getIndex() {
return tag;
}
protected final void fallBackToActivity(String tag) {
Intent intent = new Intent();
intent.putExtra(EXTRA_INDEX, tag);
setResult(RESULT_FALLBACK, intent);
finish();
}
//#Override
//public void startActivityForResult(Intent intent, int requestCode) {
// intent.putExtra(EXTRA_INDEX, getIndex());
// super.startActivityForResult(intent, requestCode);
//}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_FALLBACK) {
if(!data.getStringExtra(EXTRA_INDEX, "default_tag").equals(getIndex())) {
setResult(RESULT_FALLBACK, data);
finish();
}
}
}
}
I`m working on an app that does reading and handling specific URIs from NFC tags. I have a "reader" activity (A) registered on NDEF_DISCOVERED which reads the data from the tag and than launches a "data handling" activity (B) that operates with the data.
Currently I have three tags, each with a different URI, more specifically with the same schema and path, but with different query data --> the tagID:
T-1: mySchema://gman.com/path?id=T-1
T-2: mySchema://gman.com/path?id=T-2
T-3: mySchema://gman.com/path?id=T-3
Manifest:
<activity
android:label="#string/reader_nfc"
android:name=".reader.nfc.NfcReaderActivity"
android:stateNotNeeded="true" >
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="mySchema" />
</intent-filter>
</activity>
<activity
android:label="#string/data_manager_name"
android:name=".data.handlers.DataHandlerActivity" >
</activity>
So, getting to the problem. When I read the data from the first tag, for example T-1, the reader activity goes normally trough the lifecycle and launches the data handling activity which does its job and shows the correct output. The same thing happens when I read from the next tag (T-2 or T-3), but when I return to the first tag I get the output produced from the last-previously scanned tag.
The log shows something like this:
the ActivityManager logs the the start of the intent with the right data (from T-1) but the lifecycle of activity A doesn`t get started, instead activity B restarts and the data from the previous intent is received and handled (by calling getIntent().getData() in activity B).
When switching between T-2 and T-3 everything works fine.
I would really appreciate if someone explaind me what is going on. I saw similar behaviour when setting singleTask launch mode, but I`m not using it.
I`m developing on API v.2.3.3, testing on Nexus-S with android version 2.3.6
Thnx!
==================================================================================
EDIT: I found a solution on my problem which fits my needs, still I have a question.
I focused on the reader activity and commented out the handling and the other stuff that was going on. Here is the code snipet:
public class NfcReaderActivity extends Activity {
private static final String TAG = "NfcReaderActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.nfc_reader);
}
#Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
readAndHandleData();
}
protected void readAndHandleData() {
NdefMessage[] srcObj = readSource();
if (srcObj != null) {
Uri srcData = getSrcData(srcObj);
launchSourceManagerActivity(srcData);
} else {
Log.w(TAG, "srcObj was null!");
}
}
public NdefMessage[] readSource() {
Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] ndefMsgs = null;
// store NdefMessage-s from rawMsgs in ndefMsgs
return ndefMsgs ;
}
public void launchSourceManagerActivity(Uri srcData) {
// launches DataHandlerActivity with srcData
}
public Uri getSrcData(NdefMessage[] src) {
// returns the data from the tag rapresented as Uri
}
}
After doing this I got some extra logs (don't know why, but I guess this is not that important), and saw that when I'm returning to the first tag, the activity gets restarted (onRestart() is called), whereas in the other two cases, when scanning the second and the third tag, the activity is recreated (onCreate() is called).
When onRestart() is called and I retrieve the data from the intent (readSource method), the getIntent() method returns the same intent received when scanning the previous tag.
I just recently started developing on Android and I`m not very familiar with the concepts, so maybe this is the core problem here ;). I tried to figure this out but I just can't get to a logical explanation. If someone could explain me the workflow here I would be really grateful.
However, this is how I resolved the issue...since the reader activity can act as a singleton I've set the launch mode as singleTask
<activity
android:label="#string/reader_nfc"
android:name=".reader.nfc.NfcReaderActivity"
android:stateNotNeeded="true"
android:launchMode="singleTask" >
and done the following changes in NfcReaderActivity:
public class NfcReaderActivity extends Activity {
private static final String TAG = "NfcReaderActivity";
/**
* override onNewIntent method and store the new intent as the current intent
*/
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.d(TAG, "onNewIntent");
// set the intent as the current intent, so new data (EXTRA_NDEF_MESSAGES) can
//be accessed when calling getIntent() in readSource method
setIntent(intent);
}
This works fine for me, but I would still like to understand what exactly was happening, so any useful (of course ;) ) comments are welcome.
You might want to read up on the activity lifecycle and stack:
http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html