I'm using firebase auth in my app. I'm checking if the user is authenticated on my MainActivity just as explained on the Firebase docs and the Udacity course.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseAuth = FirebaseAuth.getInstance();
final FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null){
loadLogIn();
}
authListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if (user == null){
loadLogIn();
}
}
};
private void loadLogIn(){
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
//Flags prevent user from returning to MainActivity when pressing back button
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
When I step through the code with the debugger, everything works well, the user is null so the loadLogIn method is called, which sends the intent, but Login Activity never opens, and I get a crash because it keeps executing code that needs the user to be authenticated. The onCreate method if my login activity is never called. I cannot understand why the intent is not executing/ what is stopping it. This had been tested and worked perfectly previously.
When the intent line is executed I get the following output on the console :
I/Timeline: Timeline: Activity_launch_request id:com.example.tobias.run time:2369840341
This is my manifest if it helps :
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:name=".app.RunApplication"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".app.MainActivity"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".editor.EditorActivity"
android:parentActivityName=".app.MainActivity" />
<activity
android:name=".login.LoginActivity"
android:screenOrientation="portrait"
android:theme="#style/LoginTheme" />
<activity
android:name=".settings.SettingsActivity"
android:parentActivityName=".app.MainActivity" />
<activity
android:name=".login.NewAccountActivity"
android:screenOrientation="portrait"
android:theme="#style/LoginTheme" />
<activity android:name=".login.ForgotPasswordActivity"
android:screenOrientation="portrait"
android:theme="#style/LoginTheme"/>
</application>
This issue first appeared when I tried to sign out from my app calling
FirebaseAuth.getInstance().signOut();
the user signed out, but the intent was not sent, which left the app in an invalid state, and then it crashed seconds after trying to execute code that required a user. It was not the first time I had signed out of the app, I have done it multiple times and it worked.
This is a weird issue, I hope someone can help me find the mistake. Thanks in advance.
Problem:
//...
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
//...
Solution:
//...
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
//...
When you use setFlags you are replacing the old flags... when you use addFlags you are appending new flags. Remember, a flag is just a integer which is power of two... in binary, flags look like this: 1, 10, 100, 1000, etc... (which in this case are 1, 2, 4, 8). So, what addFlags does is appending the integer you pass using the | operator.
Related
I got a very basic splashscreen activity:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
FirebaseAnalytics.getInstance(this);
mAuth = FirebaseAuth.getInstance();
mAuth.addAuthStateListener(new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
FirebaseAuth.getInstance().removeAuthStateListener(this);
startActivity(new Intent(StartActivity.this, MainActivity.class));
finish();
}
}
});
}
This splash screen opens my MainActivity. When I close this activity this is the screenshot in recent apps:
It seems like it is making a screenshot just too late, which results in this nested recent apps screenshot. This also happen on an actual device. How can I solve this weird behaviour?
Activies manifest:
<activity
android:name="activities.StartActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="activities.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTask">
</activity>
The problem seems not the be in the launchMode. I keep having the same behaviour. Even when removing the lauchmode.
It has absolutely something to do with the callback. When starting the activity directly, there is no problem.
EDIT:
I kind a found a solution. Using a delay in starting the activity resolves it. Although a hardcoded delay isn;t that nice.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(StartActivity.this, MainActivity.class));
finish();
}
},500);
EDIT 2
I do not get this behaviour when directly starting the activity. So it probably has something to do with the callback. I'm using google sign-in. It looks like some transparent activity is being closed. Could this be the Google Sign-in Activity?
Instead of calling finish(); you can use the android:noHistory="true" in your app manifest like this:
<activity
android:name=".SplashActivity"
android:noHistory="true" />
Or you can use FLAG_ACTIVITY_CLEAR_TOP in your Intent:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Edit:
Why do you use the android:launchMode="singleTask"?
You should probably change this to android:launchMode="singleInstance" or remove this line completely.
For more information, here is a great article about launch modes
I guess that your problem occur due to using launchMode singleTask without taskAffinity.
Let's change the AndroidManifest.xml
<activity
android:name="activities.StartActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="activities.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:taskAffinity="your_any_string">
</activity>
hope it help!
I was not able to reproduce the issue you are describing, but since you say that using Handler.postDelayed() helps, try the code below.
By adding/removing the listener between onResume()/onPause(), you also avoid starting an activity when your app is in the background. Also, if the FirebaseAuth holds a list of listeners, it will keep references to activities that were already destroyed if hits the onCreate() method multiple times, for example on rotation. So you can avoid memory leaks.
private FirebaseAuth.AuthStateListener authStateListener;
#Override
protected void onResume() {
super.onResume();
FirebaseAnalytics.getInstance(this);
authStateListener = new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
FirebaseAuth.getInstance().removeAuthStateListener(this);
authStateListener = null;
startActivity(new Intent(StartActivity.this, MainActivity.class));
finish();
}
}
};
FirebaseAuth.getInstance().addAuthStateListener(authStateListener);
}
#Override
protected void onPause() {
super.onPause();
if (authStateListener != null) {
FirebaseAuth.getInstance().removeAuthStateListener(authStateListener);
}
}
In my application I have several "intents" that I use to transition between different activities in my application. I have noticed a strange behavior that occurs on Samsung devices - but not on Nexus devices - whenever a new intent is created the application launches a second "task" for this new activity! When the user goes to the multi-tasking menu they can see multiple copies of the application! This is not the desired behavior. Any and all advice would be greatly appreciated!
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:launchMode="singleInstance">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
</activity>
<activity
android:name=".Settings_area"
android:screenOrientation="portrait" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyDieXTCaFoIL0kJ_IM4UMBSQL3sNn92AWM" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" />
<activity android:name=".Splash"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".aboutPageActivity" />
<activity android:name=".turnOffFromNotification"
android:noHistory="true"></activity>
</application>
I have already attempted removing the launch modes as well as changing the application launch mode to singleTop and standard.
Intent that creates a second instance:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, splashDisplayLength);
return;
}
Intent that creates a third instance:
public void goToAboutPage()
{
Intent goToAboutPage = new Intent(this, aboutPageActivity.class); //create the intent to go to the map screen
startActivity(goToAboutPage); //actually go to the map screen
}
A third instance can also be created from launching a settings intent:
public void changeToSettingsScreen() //changes the screen to the setting screen
{
readyToSendPackets = false;
sendSwitch.setChecked(false);
// textView.setText("NOT sending"); //set the textview to advise users packets are not being sent
Intent goToSettings = new Intent(this, Settings_area.class);
startActivity(goToSettings);
}
I also over rode the onNewIntent Method:
protected void onNewIntent(Intent intent) {
// super.onNewIntent(intent); //REMOVED THIS TO AVOID DOUBLE INSTANTIATION ON TOUCHWIZ IF ANYTHING BREAKS LOOK HERE FIRST
setIntent(intent); //this allows us to recieve the extras bundled with the intent
// System.out.println("Here is the bindle: " + getIntent().getExtras());
if (getIntent().getExtras() != null) //check to see if there are any extras, there wont be on apps first start
{
Bundle extras = getIntent().getExtras(); //get the extras
String methodName = extras.getString("methodName"); //assign the extras to local variables
if(methodName != null && methodName.equals("turn_send_switch_off"))
{
sendSwitch.setChecked(false);
}
//else if(**other actions that may need to be performed can go here**)
}
Thank you very much for any help!!!
Usually if you have to force a single instance of the app, you should avoid putting android:launchMode="singleInstance" on each activity seeing as it would try to launch an instance for each activity.
Removing the launchMode from everything except the application should ensure that only the application runs in a single instance, although what #Shaishav said is true, most of the time you can let android deal with the lifecycle of an application by not setting the launchMode, unless you have a real need to ensure only one instance is running at a time.
For now, the InitialActivity is set as the main activity in the AndroidManifest.xml file.
<activity
android:name=".ui.SplashActivity"
android:noHistory="true" >
</activity>
<activity
android:name=".ui.InitialActivity"
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=".ui.LoginActivity"
android:noHistory="true" >
</activity>
<activity
android:name=".ui.RegisterActivity"
android:noHistory="true" >
</activity>
<activity
android:name=".ui.MainScreenActivity"
android:launchMode="singleTop">
</activity>
After finishing the registration process in the RegisterActivity, quit the application and re-start the app, I want the app to directly go to MainScreenActivity automatically logged in. So here's the part of the InitialActivity class that checks if I am logged in and goes to the MainScreenActivity if I am.
session = new SessionManager(getApplicationContext());
// check if user is already logged in.
if(session.isLoggedIn()) {
// User is already logged in. Take him to MainScreenActivity.
Intent intent = new Intent(InitialActivity.this, MainScreenActivity.class);
startActivity(intent);
finish();
} else {
startActivity(new Intent(this, SplashActivity.class));
}
So far, I managed to go directly into the MainScreenActivity with the code above, but in this case I miss the splash. How can I modify the code so that I can still see the SplashActivity even after being automatically logged in?
FYI here's the SplashActivity class code.
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 1500);
}
}
I will use the splashActivity as the launcher activity (it will be launch every time) and I will check in this activity if user is logged or not, depending on the response launch the RegisterActivity or the mainActivity
I am trying to achieve following case on Android, but no success:
1) Launch Application (Launcher Activity which is a subclass of Base Activity). The Base Activity has code as follows:
///This is in BaseActivity
#Override
public void onCreate(Bundle instance)
{
super.onCreate(instance);
//Config.isLoggedIn() is a static function.
if(! Config.isLoggedIn())
{
////Config.startLoginActivity is a static function
Config.startLoginActivity(this, getIntent());
finish();
}
}
The Config.startLoginActivity functions is defined as
public static void startLoginActivity(final Context ctx, final Intent finishIntent)
{
Intent i = new Intent(ctx, ItemListActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("FINISH_INTENT", finishIntent);
ctx.startActivity(i);
}
Now, the ItemListActivity contains a list of Items as {Item1, Item2, Item3}. In ItemListActivity, I am saving the passed "finishIntent" as
///This is ItemListActivity onCreate Method
if(getIntent().hasExtra("FINISH_INTENT"))
mFinishIntent = getIntent().getParcelableExtra("FINISH_INTENT");
and the onItemListSelected method is described as follows :
#Override
public void onItemSelected(String id) {
Config.setLogInState(true);
if(mFinishIntent != null)
{
Log.i("ITEMLISTACTIVITY", "Class Name = " + mFinishIntent.getClass().getName());
Log.i("ITEMLISTACTIVITY", "Starting mFinishIntent Activity");
startActivity(mFinishIntent);
finish();
}
}
But the issue is the Main Activity is not being launched again, Android takes me to the home screen instead. While looking for a solution, I saw that Google I/O app has the same implementation and that works flawlessly but in my case it is not. I am unable to figure it out. Please help.
Thanks in Advance.
Manifest File is as follows :
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.app.myapplication.ItemListActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name="com.app.myapplication.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>
Ok Here is a quick help which works for 100 percent which I'm using not mostly but EVERYTIME! you must past it through intent and in your case here it is how it must look like.
Intent intent = new intent(//name of your activity in which you are at the moment.this, //name of activity to which you want to go.class);
startActivity(intent);
Hope this will help
Im writing a program that offers a quick reply dialog upon receipt of an SMS.
However, I am getting an unexpected result. When I receieve an SMS, the appropriate dialog activity comes up displaying the correct phone number and message, however there is a second activity behind it that is the 'default' activity in my program (it is what opens when i launch my application)
I do not want this second activity to come up. The quick reply activity should come up by itself over top of whatever the user was doing before.
The 'floating' activity:
public class quickReply extends Activity {
String mNumber, mMessage;
TextView mMainText;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMainText = (TextView)findViewById(R.id.mainText);
try{
Intent i = getIntent();
Bundle extras = i.getExtras();
mNumber = extras.getString("theNumber");
mMessage = extras.getString("theMessage");
this.setTitle("Message From:" + mNumber);
mMainText.setText(mMessage);
} catch(Exception e) {
mMainText.setText(e.getMessage());
}
}
}
The call to the activity inside an onReceive()
Intent i = new Intent(context, quickReply.class);
i.putExtra("theNumber", mNumber);
i.putExtra("theMessage", mMessage);
i.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
The Manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".quickReply"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name=
"android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
the only way I have found that works, in your activity definition in manifest:
android:launchMode="singleInstance"
but then you have to relaunch your main/default activity once the dialog is dismissed. NOTE: you will lose all state from the previous launch, so this is a less than ideal solution.
UPDATE:
you can also do this by:
Intent.FLAG_ACTIVITY_CLEAR_TASK
so here's what I did:
open the original/main activity
from a service, launch the dialog style activity using the above (main goes bye-bye).
when the user dismisses the dialog, start main again with an extra intent (IS_BACK) that is processed in onCreate() and calls:
moveTaskToBack(true);
this will keep the task under the dialog on top and your main in the back of the stack.
You should set the task affinity of the activity to something different than your main activity. This will separate it from the main activity and it will track as a separate task:
<activity android:name=".quickReply"
android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog"
android:launchMode="singleTask"
android:taskAffinity="quickReply"
>