Android Program Flow Help - android

Consider the following:
I have three packages:
package.a
package.b
package.c
Each package contains a class and an activity
package.a>>activity.a>>class.a
package.b>>activity.b>>class.b
package.c>>activity.c>>class.c
I am assembling all of these together into one application and I want activity.c to be the first activity in the stack. In other words, I want it to run first. Do I simply modify the order in the manifest? What initializes the first application and what sets their corresponding order? After the first activity is started can I just go back and forth with intents?
Thanks for your help!

You want to use the CATEGORY_LAUNCHER intent filter on the activity you want to be launched when the application starts.
From the documentation:
CATEGORY_LAUNCHER The activity can be the initial activity of a task and is listed in the top-level application launcher.
Once the application is started, you can move back and forth using intents.

You need to set your activity C with this intent filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This way that will be the only activity launched from the home screen.

Related

Changing Mainactivity in Android

In iOS it is very use (when using storyboard) to change root view controller.
Root view controller; Controller that loads/appears to user when user first opens the app.
Is there a way to do that in android. I have an activity (e.g. RegistrationActivity) and I want that activity to be first activity that gets loaded in android
I can go to RegistrationActivity by following a flow, but for purposes of debugging I want to short circuit thsoe steps and want my emulator to load that activity on app launch.
You will notice that within the AndroidManifest.xml the MainActivty declaration has the intent filter
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Simply move this to which ever activity you desire to be the root activity

Start External Activity for Result and Return to Requesting Activity

I have two applications, call them app 1 and app 2, and am starting an activity in app 2 from app 1 for a result.
The activity in app 2 has an activity-alias applied to it so I can add an action (used to start it), and requires a signature-level permission (shared with app 1).
The code used to start the app 2 activity from app 1 is simple:
Intent startActivity = new Intent(MY_CUSTOM_ACTION);
startActivity.setPackage(APP2_PACKAGE);
startActivity.putExtra(EXTRA_SOME_PARAM, ...);
startActivityForResult(startActivity, 1234);
This results in the activity appearing as expected. I then get some user input, and clean up app 2's activity like so:
setResult(Activity.RESULT_OK, someData);
finish();
What I am expecting to happen, is for the calling activity in app 1 to be restored to the foreground and so onActivityResult to be called, but what is actually happening, is that app 2's main/launch activity is started instead.
Sometimes I get the onActivityResult callback with the right response code and data, but app 1's activity is still stopped.
My understanding of Android led me to believe that app 2's activity would have been part of app 1's task stack (and opening the task switcher bears this out), and so finishing it would just pop back to the caller.
I've tried:
enabling task reparenting on the app 2 activity
zeroing out the flags on the startActivity Intent
ensuring that app 2's activity doesn't try to start any other activities
tracing through the Activity lifecycle to see what's happening, but all trails go cold at NativeActivityManager...
clearing any existing tasks that app 2 has open
finishing app 2's activity with finishAffinity (doesn't work when returning a result)
None of this seems to have an effect. Many of the Intent flags and launch modes aren't allowed when starting for a result, so I'm at a loss for what to try next.
Thanks!
Here's the <activity> definitions from the manifest in case it matters:
<activity
android:name=".activity.App2Activity"
android:windowSoftInputMode="adjustResize"
android:configChanges="locale"
android:theme="#style/Some.Theme"
android:allowTaskReparenting="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity-alias
android:name=".activity.App2ActivityAlias"
android:permission="x.y.z.MY_CUSTOM_PERMISSION"
android:targetActivity=".activity.App2Activity"
android:exported="true">
<intent-filter>
<action android:name="x.y.z.MY_CUSTOM_ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity-alias>

android "which application you want to use" dialog when redirecting to an activity

these are the 2 intent filters of my 2 activities:
StartActivity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
HomeActivity
<intent-filter>
<action android:name="android.intent.action.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I'm in the home activity and when I call:
activity.startActivity(new Intent("android.intent.action.MAIN"));
Android shows a dialog that askes which application I'd like to use to go on.
Someone is so kind to explain me why??
EDIT
I'm gonna be clearer:
StartActivity -> Contains login e and signin actions
HomeActivity -> Contains the most part of the app
My scenario:
I successfully logged in and now I'm in the HomeActivity, but when I log out and redirect to start activity android shows a dialog "With wich application do you wanna complete this action?" (or something like this) instead of redirecting me to the StartActivity
I don't know why you need a Action HOME but it looks like your problem is the Category DEFAULT. Due to the android docs this category is defined as :
Set if the activity should be an option for the default action (center press) to perform on a piece of data. Setting this will hide from the user any activities without it set when performing an action on some data. Note that this is normally -not- set in the Intent when initiating an action -- it is for use in intent filters specified in packages.
Constant Value: "android.intent.category.DEFAULT"
So I think changing this one to the right one which fits your need will solve the problem. Just a suggestion!
Read more at: http://developer.android.com/reference/android/content/Intent.html#CATEGORY_DEFAULT
Not sure if it's resolved, but I avoided the problem using
activity.startActivity(new Intent(activity,StartActivity.class));

Newly added Activity designated as Main is not always "Main" Activity shown

Allow me to explain my predicament.
I have been building an app for a while now where it would immediately enter my MainActivity.class. I had this Activity declared in the AndroidManifest.xml file as such:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Then I realized that I wanted to add a Login Activity to my app so a NewMainActivity.class was required. I've been able to successfully implement this new Activity. And I changed the <intent-filter> of the old MainActivity.class to the following:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
However this is where my problem arises.
When I press the back key on the Login Page (NewMainActivity.class) it goes through the onPause(), onStop(), and onDestroy() methods. When I start the App again the Login Page (NewMainActivity.class) is not shown, the old MainActivity.class is shown instead.
Can anyone guess what I'm still missing/doing wrong? It seems to me like it should always start on the Login Page everytime now. Let me know if a code sample would help.
Delete from your old MainActivity.class the intent-filter that you've defined:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Update: I've understood your question incorrectly at first. The intent-filter I've proposed to delete does nothing except exporting your activity. This means that any application can call the activity of your MainActivity.class So, in any case it should be deleted.
Now about the login activity. As I understand your scenario is the following. When you start your application it at first starts login activity. Then you enter your credentials and the main activity is appeared if the login process has been successful.
Thus, you have two activities in the stack: login and your main class. When you then push home button and run your application once again it starts from the last activity in the stack i.e. Main activity.
Thus, the problem is not in your intent-filters. You should finish your activities. In this case you'll always start from the login activity.
If at first Yury's suggestion does not work, and you did something similar to what I did, which was copy and pasting your workspace between multiple computers and loading the project to eclipse each time, then simply start a new project and copy the actual text within all your classes to fresh new classes in the new project.

Help with first Android Activity

When my app first opens my first activity that is presented to the user can vary based on configuration options. I only know how to hard code the first activity that runs when the app is running by adding something like this in the Manifest
<activity android:label="#string/app_name" android:name=".MyFirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Where MyFirstActivity is the class name of the first activity class to be run. How can I dynamically select which activity to run first when the app is first launched rather than hard code it in the manifest?
Thanks!
Option #1: In onCreate() of MyFirstActivity, call startActivity() for the right activity, then finish().
Option #2: Define several activities with the LAUNCHER <intent-filter>, all but one disabled. On first run (or as needed), enable the right activity and disable the others. Downside: may require a phone reboot to update the launcher, since not all home screen launchers will detect your change.
Option #3: Redesign your GUI such that this is not an issue.

Categories

Resources