Start Activity before MainActivity in SherlockFragmentActivity - java

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

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.

How to switch launcher activity while the application is running

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

getting an error while starting normal main activity [duplicate]

This question already has answers here:
How to start new activity on button click
(28 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am currently breaking my head off, because I can start a normal startActivity(). It is always giving me this error:
Attempt to invoke virtual method
'android.app.ActivityThread$ApplicationThread
android.app.ActivityThread.getApplicationThread()' on a null object
reference
I am starting it from my Application
Welcome Activity:
application.getUserFromDatabase(getApplicationContext(), username, password);
Application:
public void getUserFromDatabase(Context context, String username, String password) {
//new GetFromDatabase().execute("getUser.php", "?username=" + username + "&password=" + password, this, context);
WelcomeActivity activity = new WelcomeActivity();
activity.startMainActivity();
}
AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.mydomain.app">
<application
android:name=".MainApplication"
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">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".WelcomeActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you in advance :)
EDIT:
I forgot adding my Method startMainActivity here:
public void startMainActivity(){
Intent startMain = new Intent(getApplicationContext(), MainActivity.class)
startActivity(startMain);
}
you must use intent for startActivity();
example:
Intent intent = new Intent(this, WelcomeActivity.class);
startActivtiy(intent);
It definitely causing you a null pointer exception because your WelcomeActivity is not created yet. You can't start an activity by creating a new instance of Activity with:
WelcomeActivity activity = new WelcomeActivity();
Your activity need to be start with:
Intent welcome = new Intent(context, WelcomeActivity.class);
context.startActivtiy(welcome);
or by adding the correct intent-filter so the activity will be launched by the Android system.
You can start your activity from Application with something like this:
// FLAG_ACTIVITY_NEW_TASK is required
// FLAG_ACTIVITY_CLEAR_TOP brings back to the top of the stack
Intent mainActivity = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mainActivity);
Though I think you should not start an Activity via Application. You need to think throughly about your design so there is no activity connection with the application.

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.

Android intent not starting new activity?

I'm trying to do something rather simple here, just launch a new activity from my main one. Here's the code:
public class mainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(mainActivity.this, testActivity.class);
startService(i);
}
}
///////////////// next file /////////////////
public class testActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Toast.makeText(this, "testActivity started", Toast.LENGTH_SHORT).show();
}
}
///////////////// manifest section ///////////////////
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".mainActivity"
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=".testActivity" />
But I never see the Toast from testActivity - what gives?
You want to use startActivity instead of startService
Intent i = new Intent(mainActivity.this, testActivity.class);
startActivity(i);
To start an activity you should use startActivity() instead of startService().
You'll also have to make sure the testActivity is listed in your android manifest.
If the activity is still running in the background, it gets called and only the onResume() gets called, not the onCreate();
check the lifecycle here: http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle

Categories

Resources