How to switch launcher activity while the application is running - java

I have an application that has two main activities MainActivity1 and MainActivity2.
I have the Intent filter for the launcher on MainActivity1
activity android:name=".MainActivity1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
what I want is to be able to switch the launcher activity in my settings activity.
Is that possible and how will it work?

All I did was to create a shared preference boolean the know which activity to open. Then from MainActivity1 I used an intent to go to MainActivity2
Intent intent = getIntent();
boolean firstTimeOpening = intent.getBooleanExtra("BOOLEAN_FIRST_TIME_OPENING", true);
SharedPreferences preferences = getSharedPreferences("SHARED_PREFERENCES", MODE_PRIVATE);
boolean startAppWithActivity1 = preferences.getBoolean(START_APP_WITH_ACTIVITY1, true);
if(!startAppWithActivity1 && firstTimeOpening){ startActivity(new Intent(MainActivity1.this, MainActivity2.class));
}
Easy enough. The firstTimeOpening boolean makes sure the the switching
of activity occurs only when the application is just opened

Related

StartActivity not working inside onPause in Android

I always want to show password activity when my Android app is on pause.
For example, when the user goes to home screen or switches app using Recents screen, password activity should appear right away to hide contents and protect to resume app. However, startActivity is not working as expected inside onPause method.
override fun onPause() {
super.onPause()
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}
It actually executes startActivity when the App goes onPause. However, the onCreate of MainActivity2 is not being called right away, but called when the app resumes. It was same inside onStop by the way.
Any solution or idea for this?
Edit 1
AndroidManifest.xml
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/Theme.Droid"
tools:targetApi="31">
<activity
android:name=".MainActivity2"
android:exported="false"
android:label="#string/title_activity_main2"
android:theme="#style/Theme.Droid">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onPause() {
super.onPause()
val intent = Intent(this, MainActivity2::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
}
}
MainActivity2.kt
class MainActivity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
}
These are the example code that I've tried, but still doesn't work. The process I've tried is like this. I wish that the onCreate of MainActivity2 is called right after the Home key is pressed.
Launch app → Home key → Recents screen (Doesn't show MainActivity2) → Resume app (onCreate of MainActivity2 is called)
Startin activty in onPause() is not recommended but even then if you want to start it, then use FLAG_ACTIVITY_NEW_TASK while starting. This tells system to start new activity in new instance so that when current activity gets is killed by system, the new activity has time to start since its in another instance.
Here is the code :
#Override
public void onPause() {
super.onPause();
Intent intent = new Intent(this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
This should help you.

why does my main activity show's first than my splash screen

this the first part of the code with the splash screen code
public class MainActivity extends AppCompatActivity {
private static final int SPLASH_TIME_OUT = 1000;
private static final String FILE_NAME = "example.txt";
EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = findViewById(R.id.edit_text);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent homeIntent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(homeIntent);
finish();
}
},SPLASH_TIME_OUT);
the first thing that comes out after i run the app is the main activity and not the splash screen
You have to specify your activity Actions by using Intent Filter ,
Intent Filters specifies the types of intents that an activity, service, or broadcast receiver can respond to. An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.
As in For your Case, You have to specify that MainActivity/SplashActivity should be on Launcher Mode(Launcher mode is an instruction for Android OS which specifies how the activity should be launched), you have to use Intent filter and specify Activity Actions ,
*Must go through this Article App Manifest Overview
Add this in your Manifest file within <Application> Tag
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
In your AndroidManifest file, SplashActivity must be set as Launcher activity. Check the name of the Splash screen activity file in your project.
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Can you share your manifest? Launch Activity should be Splash Activity.
Which Activity you want the application to start with, you need to add IntentFilter in that Activity in the Manifest file.
<activity android:name="SplashActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

language don't change in landscape mode android java

i am having hard time in this do not know why this is happening
my android manifest
<activity
android:name=".Splash"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="locale|keyboardHidden|orientation|screenSize"
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.DEFAULT" />
</intent-filter>
</activity>
now when i load my MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
orienta(this);
setLanguage(this, getLanguage(this, "en"));
setContentView(R.layout.activity_main_activity);
now these function of orientation and language code are as follows
public void orienta(Context c){
SharedPreferences spp = PreferenceManager.getDefaultSharedPreferences(c);
String ss = spp.getString("orientation", "portrait");
if (ss.equals("portrait")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
if (ss.equals("landscape")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
private String getLanguage(Context c, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
return preferences.getString("language", defaultLanguage);
}
#SuppressWarnings("deprecation")
public void setLanguage(Context context, String languageCode) {
Locale locale = new Locale(languageCode);
// Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
} else {
config.locale = locale;
}
context.getApplicationContext().getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("language", languageCode);
editor.apply();
}
now the problem is that
in portrait mode every activity shows the language which is selected in settings
but in landscape mode the language is only shown in fragment and navigation menu items,
in preferenceactivty and other activities and also in toolbar it is same english language how to fix it
i have added
android:configChanges="locale|keyboardHidden|orientation|screenSize"
in each and every activity defined in manifest
EDIT :-
by putting in every activity before setcontentview make landscape mode works
don't know why this issue is coming
if anybody get this issue in future and fixed it please comment
only coming in landscape mode
well by putting setlanguage method call in every activity fix issue on landscape mode ,well portrait mode works fine just calling it in mainactivity but to make language works in landscape mode it needs to call in every activity
in future is someone get this issue or have fix please comment

ImageButton not responding

What I am attempting to do is to open "Items" when the ImageButton "ibItem1" is pressed. But, after setting up this:
ibItem1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent items = new Intent("android.intent.action.ITEMS");
startActivity(items);
}
});
And clicking the button does not do anything. I have the activity all set up in the manifest:
<activity
android:name="com.example.custombuilds.Items"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ITEMS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The program has crashed before because of a ActivityNotFoundException, even though I declared it in the manifest.
Intent myIntent = new Intent(this, NextActivity.class);
startActivity(myIntent);
In this form, it should work..
Also make sure that the name of the class that implements the activity is the same in the manifest file at the android:name="atributevalue".The attribute value should be a fully qualified class name.

Start Activity before MainActivity in SherlockFragmentActivity

I have 2 Activitys
MainActivity
LoginActivity
I want verify if user has logged, else show my LoginActivity before the MainActivity apears.
When I put this in OnCreateView of MainActivity I have an error:
if(Login.isLogged()){
selectItem(0);//starts the fragment
}
else {
Intent i = new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
}
FATAL EXCEPTION: MAIN java.lang.RuntimeException: Unable to start activity ComponentInfo { .....MainActivity}: java.lang.NullPointerException
Ps: My MainActivity is an SherlockFragmentActivity. I'm using DrawerLayout.
How can i start my LoginActivity before SherlockFragmentActivity?
Thanks
If I were you I would put LoginActiviy as launcher activity. There I would check if user has already logged in, if he has start new activity.. Something like this:
//in onCreate of LoginActivity
if (hasLogedIn) {
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
finish();
} else {
// show him login activity, setContentView etc...
// save hasLogin boolean as true
}
make this changes in your AndroidManifest file
<activity
android:name="com.example.alphabets.LoginActivity"
android:label="#string/title_activity_login"
android:windowSoftInputMode="adjustResize|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.alphabets.MainActivity"
android:label="#string/app_name" >
</activity>
Make Login Activity as your default activity. Check Login status. If user is logged in, call Main Activity and finish() the Login Activity. else, wait for user input

Categories

Resources