A white screen blinks for few milliseconds when I simply go from login activity to Main activity with following clear back stack code.
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
overridePendingTransition(0, 0);
If I just make the startActivity without Flags (or say without clear backstack)
overridePendingTransition(0, 0);
It doesn't blink with white screen.
But I have to clear back stack and start a new activity with NO transition animation. So it doesn't blink/appear in white screen for few milliseconds.
Looking forward to get a perfect answer soon. Cheers !
Try setting noHistory="true" for login activity in the manifest and start HomeActivity without any flags. This should solve your problem.
Don't use Intent.FLAG_ACTIVITY_NEW_TASK. If you want to clear the backstack (and assuming your login Activity is the only one in the stack), just finish the current Activity:
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
startActivity(intent);
overridePendingTransition(0, 0);
finish();
Intent.FLAG_ACTIVITY_NEW_TASK is likely the cause of the flash since it requires you to create a whole new "task" (which is a collection of Activities with a given history) rather than just reusing the current one. It's almost like navigating to another app at that point.
As I have mentioned in the question that startActivity without using flags makes no blink and showing white screen.
So I choose that.
Now the problem is to clear the stack of 1st Login activity when I am into Main Activity.
I resolved it by setting the context of Login activity into Application class using setter and getter method.
And Whenever You reached to Main Activity. It will check whether you have the context in Application class by getter method.
If it's a yes and is a login activity then make the context.finish which will clear the back-stack by removing the login activity from Main Activity. And set null value to setter in Application class.
Feel free to share any concern regarding this. Thank you.
Related
I have been trying to figure out why my Intent would not transfer string data from one activity to another activity? I seems I had set launchMode = singleTask in the manifest folder and when I changed launchMode to standard the Intent code worked as expected.
The MainActivity is the first activity in the stack I am guessing that I made the setting a number of months ago to try and prevent the user from using the back button to navigate back to the password log in page. (MainActivity)
I kind of get the Back Stack idea but WHY would this setting inhibit the intent from transferring data. my test for transfer was a System.out.println statement?
Suppose you have activities A and B. A is the one with android:launchMode="singleTask". A starts B. B then starts A, causing the existing instance of A to return to the foreground.
In that case, A is called with onNewIntent(), and that Intent will have the extras from B.
onCreate() is only called when an activity is created.
I am writing a math app for little kids to learn maths. It first prompts the user to select what kind of questions they want (MainActivity), and then it shows a bunch of questions (QuestionsActivity). After answering 10 questions, it tells you which question(s) did you answer correctly and which you didn't (ResultsActivity).
I know that Android puts all the activities on a stack. In my case, it would look like this:
ResultsActivity
QuestionsActivity
MainActivity
And when you call finish, an activity is popped from the stack. I want there to be a back to main menu button in the ResultsActivity to go back to the MainActivity. However, if I call finish in the ResultsActivity, the user would see QuestionsActivity! So how am I going to call finish on the both activities?
Two options:
Call finish() in QuestionsActivity after you make the call to start the ResultsActivity. This will remove it from the stack so that pressing back from ResultsActivity returns to MainActivity.
Use Intent.FLAG_ACTIVITY_CLEAR_TOP in the intent to go back to MainActivity. This will clear all activities that are on top of it.
You can clear your stack by simple starting your MainActivity again and clearing the stack with the following flags:
final Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Learning how to make android apps and I did this tut. Summary of the tut is here:
http://sketchytech.blogspot.com/2012/10/android-simple-user-interface-activity.html
I'm trying to figure out how intents work. In the tut you create an Intent called intent, and in DisplayMessageActivity.java it creates an Intent called intent by calling "getIntent()".
Does the "getIntent()" function (or method (I'm most familiar with C)) just return the most recently created intent? Can there only be one intent at a time?
Thnks in advance for any responses!
All Activities are started by either the startActivity(Intent) or the startActivityForResult(Intent, int) methods. The intent tells the Activity everything it needs to know to display the correct information at launch. getIntent(), when called in an Activity, gives you a reference to the Intent which was used to launch this Activity.
getIntent() method gets the intent that called this activity.there can be more than one intent but you have only one intent visible at one time (since only one activity is visible at one time)
An Activity is typically created through an Intent. Assuming you are in your first activity:
Intent intent = new Intent(MyFancyActivity.class, Intent.ACTION_VIEW);
startActivity(intent);
This launches a new MyFancyActivity instance. From MyFancyActivity, you can retrieve the intent which lead to that instance creation. That is, the getIntent() method:
// this is the intent created in your first activity
Intent i = getIntent();
There are two primary forms of intents you will use.
Explicit Intents have specified a component (via
setComponent(ComponentName) or setClass(Context, Class)), which
provides the exact class to be run. Often these will not include any
other information, simply being a way for an application to launch
various internal activities it has as the user interacts with the
application. Implicit Intents have not specified a component; instead,
they must include enough information for the system to determine which
of the available components is best to run for that intent.
An Intent is a data class which holds information for an Activity that is about to be started. An Activity is a manager or controller for the view which is currently displayed on the screen.
Activities in the system are managed as an activity stack. When a new
activity is started, it is placed on the top of the stack and becomes
the running activity -- the previous activity always remains below it
in the stack, and will not come to the foreground again until the new
activity exits.
I implemented a button in my app that clears all sharedpreferences using this code:
context.getSharedPreferences("bifrostPrefs", 0).edit().clear().commit();
Now the problem is that whenever I use the button, I then need to exit the activity and re-open it to see results. I tried solving this by simply making the button re-open the activity with this code:
Intent reOpen = new Intent (Bifrost.this, Bifrost.class);
startActivity(reOpen);
My idea seemed smart until I noticed that if I re-open the activity, I then need to press the back button twice to get back to main activity. So I did some reserach and found this code:
finish();
startActivity(getIntent());
This now works fine, the activity gets refreshed and then I only need to click the back button once. But is there another way to refresh activity without it "flashing" in and out? As you know, everytime you open a new activity, it flashes in and out so the app lags for a second. Is there a way to refresh an activity by bypassing this?
Well, it would be better to update the content of the activity, but if it's too complicated you can override the default animation with this method :
finish();
overridePendingTransition( 0, 0);
startActivity(getIntent());
overridePendingTransition( 0, 0);
You can add the flag Intent.FLAG_ACTIVITY_NO_ANIMATION (link) to your reOpen intent to eleminate all animations. But as stated from the other answer better refresh the data inside you Activity.
do you have some views that may change their value/size based on values from your shared preferences? if yes, create a method that init the views and call that methon on onCreate() method and in onClick() method.
In my application when i click the back button it passes through all the activities that i open them previously , i used the public void onBackPressed() method to make the back button back to the activity that i want as follow
public void onBackPressed()
{
startActivity(new Intent("com.MyDiet.Main"));
Tracker.this.finish();
}
is that true and safe way to code the back button ? how i can prevent the application from passing through all the previous opened activities when the back button is pressed ? and how i can make the application exit when i click the back button in the main activity?
In your application, and in ALL android applications, unless it's critical not to pass through unneeded steps (such as login if you're already logged in), it's VERY important not to override Android standard behaviour. Users normally complain about Android apps not having a common behaviour or style guideline.
Anyway, yeah, you can just override onBackPressed on all your activities and do whatever you want. But just don't.
This approach isn't good, because you're polluting the activity stack of your application
Example:
current stack: MainAct -> Act2 -> Act3 (we're in activity 3)
With your code above, when you press back, the stack now looks as follows:
MainAct -> Act2 -> MainAct
Because you ended Act3 and launched a NEW main activity, which may be not what you wanted.
To achieve what you want (Get back to main when the current activity is over) you need to work on the intermediate activities: In the example above, when from Act2 you call startActivity("Act3"), you should call "this.finish()". Therefore you don't have to override "onBackPressed()" of activity 3: simply the default value will terminate Act3 and the next activity in the stack will be MainAct
MainAct -> A2 (A2 launches A3 and then calls this.finish())
MainAct -> A3 (user now press back)
MainAct (the mainactivity is now on top)
To summarize, you don't have to override onBackPressed, you just have to correctly manage the lifecycle of the activity between the main one and the current one.
Generally speaking it's not recommended to make things work like user doesn't expect and that is considered as very bad practice. That's why it is not good to start activity from overriden onBackPressed().
When user press back by default activity will finish and one from back stack will be displayed. You as developer will (by writing code) decide which one is that. You can do it this way but later (or for somebody else) it will be a bit messy and difficult to find this unusual place for code which is starting other activity.
So..It would be useful to read about activity lifecycle and back stack to get impression how it works and understand terminology better.
If you want one of your activity not to stay on back stack you can add in manifest file file android:noHistory="true" for that activity.
Same thing you can achieve from code by adding appropriate flag to intent when you start activity: Intent.FLAG_ACTIVITY_NO_HISTORY
When user "go away" from ActivityOne (started using intent or defined in manifest like described), to new ActivityTwo and then press back, it will not go to ActivityOne because it will not be on back stack. Using this you can precisely control navigation through your activities.
More flags are available for use, but I guess this is what you wanted to achieve.
Hope you will find my answer useful. Cheers..
You can use a lot of tricks to exit your complet application for my part i use this start a intent and then from the androidmanifest i choose the category to home and than close the current activity such would be your mainactivity !
public void onBackPressed() {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}