how can first activity be chosen at run time based on data? - android

i'm just starting out with Android and i think i'm missing something.
It seems like in Android you decide at development time which activity will be the first to be displayed in your application.
i would like to write my application in such a way that some kind of a centralized controller starts executing and it decides which activity should be first
(for example, based on some data obtained from somewhere)
is that possible to do, and if so, how?
thanks.

Most folks do it by launching an activity that just picks up the config it needs and then starts up the "real" activity. One hiccup is that the activity first launched will be on the task stack, but if you set android:noHistory="true" for the initial activity the process should be invisible to the user.

The below method can be used for showing tutorial screens on first app launch.
AndroidManifest.xml:
<activity android:name=".activities.LaunchActivity"
android:noHistory="true"
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>
<activity android:name=".onboarding.OnboardingActivity"/>
<activity android:name=".activities.MainActivity"/>
LaunchActivity.java:
public class LaunchActivity extends Activity {
public static final String FIRST_APP_LAUNCH = "com.your.package.name";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFirstAppLaunch()) {
setFirstAppLaunch(false);
startActivity(new Intent(this, OnboardingActivity.class));
} else {
startActivity(new Intent(this, MainActivity.class));
}
finish();
}
private boolean isFirstAppLaunch() {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
return preferences.getBoolean(FIRST_APP_LAUNCH, true);
}
private void setFirstAppLaunch(boolean value) {
SharedPreferences preferences = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(FIRST_APP_LAUNCH, value);
editor.apply();
}
}

I wonder What so tough in this. in the main Activity in the onCreate Method after checking the data starting another activity without setting the view content of Main Activity.

Related

Possible to prevent app launch by NFC intent from within an app?

I have an app for which the requirements are to launch it on detection of a non-NDEF NFC tag, so I'm using the TECH_DISCOVERED filter on my main activity to do so:
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
This works fine, however some users complain that their phone case doubles as a holder for their credit cards / smart cards and hence the app is unintentionally launching when they close their phone case. These users don't want to have to disable the device NFC setting (and that can't be done programmatically) so my question is: is it possible to programmatically stop an app launching by NFC intent from within that app?
The best idea I can come up with is to have the NFC intent launch a non-UI Activity (one that doesn't call setContentView) and have this check if some persistent flag has been set (by a UI control in the main activity) and if the flag is set, do not launch the main activity.
Is there an easier/more elegant solution?
The app could simply try to check whether the "discovered" tag belongs to the app (i.e. the datastructure is as expected, resp. the TagType is as expected), and if not stop again. Whether you make that visible to the use or not is up to you ...
My solution to this was to launch a headless (invisible) activity via the NFC intent and use a shared preference (set by a UI switch via the main activity) to determine whether to launch the main activity.
AndroidManifest.xml:
<activity android:name="com.mypackage.NFCLaunchActivity" android:theme="#android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="#xml/nfc_tech_filter" />
</activity>
res/nfc_tech_filter.xml:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.NfcA</tech>
</tech-list>
</resources>
MainActivity.java:
public static String SETTINGS_NAME = "settings";
public static String shouldLaunchByNFC = "launchWithNfc";
// Call on changing UI state
protected void setShouldLaunchByNFC(boolean enableLaunch) {
setSettingBoolean(this, shouldLaunchByNFC, enableLaunch);
}
// Call to set initial UI state
protected boolean getShouldLaunchByNFC() {
return getSettingBoolean(this, shouldLaunchByNFC, true);
}
public static void setSettingBoolean(Activity activity, String name, boolean value){
SharedPreferences settings = activity.getSharedPreferences(SETTINGS_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(name, value);
editor.commit();
}
public static boolean getSettingBoolean(Activity activity, String name, boolean defaultValue){
SharedPreferences settings = activity.getSharedPreferences(SETTINGS_NAME, MODE_PRIVATE);
return settings.getBoolean(name, defaultValue);
}
NFCLaunchActivity.java:
import static com.mypackage.MainActivity.getSettingBoolean;
import static com.mypackage.MainActivity.shouldLaunchByNFC;
public class NFCLaunchActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView explicitly omitted
boolean launchWithNfc = getSettingBoolean(this, shouldLaunchByNFC, true);
if(launchWithNfc){
Context context = this.getApplicationContext();
Intent intent = new Intent();
intent.setClassName(context, context.getPackageName() + ".MainActivity");
context.startActivity(intent);
}
finish();
}
}

Checking first run of Activity, and suppressing another activity in Android

I'm making a launcher application with another settings activity to tweak the launcher.
Now, i don't want the launcher to be displayed as elligible upon pressing the home button until the user has not set it up up first (they will be asked to do that once the app downloads via notifications) through the settings activity.
So, can i suppress my launcher activity from running until after first run of application, and if not, then how to know first run of an activity.
PS: I already know how to implement first run of application.
This is based on the "Settings Activity" project created using Android Studio's "Start a new Android Studio project" template. After the project is successfully created, add a new activity class (that would be your launcher activity in your current project); for this example, it is a plain empty activity.
public class HomeScreenActivity extends AppCompatActivity {
}
Then add AndroidManifest.xml entries for that activity:
<activity
android:name=".HomeScreenActivity"
android:enabled="false"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Pay attention to android:enabled="false", that is the important part here. That way your launcher activity will be disabled by default. You will change it's state after user goes through the setup process.
In this example, I simply added a SwitchPreference and changed HomeScreenActivity's state based on the user click.
private SwitchPreference prefEnableDisableHomeScreen;
private PackageManager packageManager;
private ComponentName homeScreenComponent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
packageManager = getActivity().getPackageManager();
homeScreenComponent = new ComponentName(getActivity().getApplicationContext(),
HomeScreenActivity.class);
prefEnableDisableHomeScreen = (SwitchPreference) findPreference("enable_disable_home_screen");
prefEnableDisableHomeScreen.setChecked(getIsComponentEnabled(homeScreenComponent));
prefEnableDisableHomeScreen.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object o) {
boolean previousState = prefEnableDisableHomeScreen.isChecked();
setComponentEnabledSetting(homeScreenComponent, previousState
? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
: PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
prefEnableDisableHomeScreen.setChecked(getIsComponentEnabled(homeScreenComponent));
return false;
}
});
}
private boolean getIsComponentEnabled(ComponentName componentName) {
int state = packageManager.getComponentEnabledSetting(componentName);
return PackageManager.COMPONENT_ENABLED_STATE_ENABLED == state;
}
private void setComponentEnabledSetting(ComponentName componentName, int newState) {
packageManager.setComponentEnabledSetting(componentName, newState, PackageManager.DONT_KILL_APP);
}
Hope this helps.

Launch login activity instead of MainActivity if app is on its first run

I need my app to check if it's running for first time or not. If it's the first time, then it should launch LoginActivity instead of MainActivity. And if it's not the first run, it should display MainActivity as usual.
I used SharedPreference value to check if it's available, then app decides its not running it's first run.
This is what I've tried so far
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set default values into settings
PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
// Check if the app is running on its first run
SharedPreferences fRun = getPreferences(MODE_PRIVATE);
if(fRun.getBoolean("firstrun", true)){
SharedPreferences.Editor editX=fRun.edit();
editX.putBoolean("firstrun", false);
editX.apply();
// Login activity stuff here
// Goto login screen
Intent loginIntent=new Intent(getApplicationContext(),LoginActivity.class);
startActivity(loginIntent);
//finish();
} else {
setContentView(R.layout.activity_main);
}
}
}
My problem is, when I run my app, it suddenly crashes and displays message Unfortunately, the app has stopped.
Why does the app crash? Is it because code in my LoginActivity have errors or do I need to first load MainActivity then call LoginActivity?
You can use LoginActivity as LAUNCHER activty and check whether the user is logged in. If yes, start MainActivity.
The AndroidManifest.xml:
<activity
android:name=".LoginActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
And the LoginActivity:
public class LoginActivity extends ActionBarActivity {
private static final String LOGIN_KEY = "LOGIN_KEY";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences pref = getPreferences(Context.MODE_PRIVATE);
if (pref.getBoolean(LOGIN_KEY, false)) {
//has login
startActivity(new Intent(this, MainActivity.class));
//must finish this activity (the login activity will not be shown when click back in main activity)
finish();
}
else {
// Mark login
pref.edit().putBoolean(LOGIN_KEY, true).apply();
// Do something
}
}
}
The MainActivity:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Do something
}
}
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".Activity.MainActivity" />
<activity android:name=".Activity.SignupActivity" />
<activity android:name=".Activity.SigninActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
You need to rearrange your Activity classes a bit I think. It's very simple to decide if your application has run first time or not and launch some Activity based on this decision. I would like to suggest the following architecture.
You can set a LauncherActivity to decide whether you need to start LoginActivity or MainActivity like this:
public class LauncherActivity extends Activity {
private boolean firstLaunch = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i;
SharedPreferences pref = getSharedPreferences(Constants.ApplicationTag, MODE_PRIVATE);
firstLaunch = pref.getBoolean(Constants.FIRST_LAUNCH, true);
if (firstLaunch) {
i = new Intent(LauncherActivity.this, LoginActivity.class);
startActivity(i);
} else {
i = new Intent(LauncherActivity.this, MainActivity.class);
startActivity(i);
}
finish();
}
}
You have another problem I need to sort out is calling setContentView inside an else statement which is erroneous. You need to put setContentView just after the super.onCreate(savedInstanceState); in any of your Activity.
When you're putting it inside an else statement, the content view may not be set which will cause an application crash.
So remove the checking for first run from MainActivity and move that portion to LauncherActivity which will solve the problem.
The AndroidManifest.xml of the LauncherActivity may look like this
<activity
android:name=".Activities.LauncherActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Change AndroidManifest.xml to set LAUNCHER activity base on shared preference

I have a registration activity and also a main activity.
What I want to achieve:
I want to:
1) show the registration activity if the user hasn't registered
2) show the main activity if the user has registered
What I did:
1) I have changed the registration activity to be the Main/Launcher activity in the Android.Manifest.xml file:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".RegistrationActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Problem:
1) How do I set the Main Activity as the Launcher activity after the the Register Button is pressed for subsequent app launches?
public void btnRegisterPressed (View view) {
// Save isRegistered flag into Shared Preferences
SharedPreferences userDetails = this.getSharedPreferences("userDetails", MODE_PRIVATE);
SharedPreferences.Editor edit = userDetails.edit();
edit.clear();
edit.putBoolean("isRegistered", true);
Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
You will need to make a landing page for the app and have the method run in it, if you don't want them to land into the current main. So the run method is the one to go to main.
I prefer to keep my shared preferences as constants for my app.
pref = getSharedPreferences(MyConstants.MY_APP);
editor = pref.edit();
public void run(View view) {
String getStatus = pref.getString(MyConstants.REGISTER, "nil");
if (getStatus.equals("true")) {
startActivity(new Intent(this, Main.class));
} else {
Toast.makeText(this, "Register first",
Toast.LENGTH_SHORT).show();
}
}
In your register class:
editor.putString(MyConstants.REGISTER, "true");
editor.commit();

First time shown Activity solution

I need to show one SecondActivity only once and only on first launch of the application. I implemented it like this (see below), but I don't really like a solution because I need to inflate layout on onResume() because if I do not I have an empty Activity when I click back hardware button being on SecondActivity.
public class TestActivity extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
public static final String FIRST_RUN = "FirstRun";
SharedPreferences sharedPreferences;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
if (sharedPreferences.getBoolean(FIRST_RUN, false)) {
setContentView(R.layout.main);
} else {
Intent i = new Intent(this, Second.class);
startActivity(i);
}
}
#Override
protected void onResume() {
super.onResume();
setContentView(R.layout.main);
}
}
In Second Activity I just put flag FirstRun to true.
In the first Activity call finish() after you make the call to startActivity(i)
Like this...
if (sharedPreferences.getBoolean(FIRST_RUN, false)) {
setContentView(R.layout.main);
} else {
Intent i = new Intent(this, Second.class);
startActivity(i);
finish();
}
You can then remove setContentView(...) from onResume().
The bestWay i can think is having Init activity that don't have any layout and just decides what activity to run first
If all you want is to prevent the user to go back to the activity, add the "noHistory" flag in your manifest file, like this:
<activity android:name=".SecondActivity" android:noHistory="true">
If this is your "splash screen" activity, and only needs to be shown on app start, do this:
<activity android:name=".SecondActivity" android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Categories

Resources