cannot understand the finish method android [duplicate] - java

This question already has answers here:
Is it a good idea to call finish() after starting a new Activity in Android?
(3 answers)
Closed 5 years ago.
protected void onCreate(Bundle savedInstanceState) {
//if user is already logged in open the profile activity directly
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
finish();
startActivity(new Intent(this, Home.class));
}
buttonSignIn.setOnClickListener(this);
buttonSignUp.setOnClickListener(this);
}
Can someone please explain to me why is finish() called before launching the Home.class if the user is already logged in. I'm trying to go through some source code and not able to understand this.

simply when you call finish();
its more like an intent which do this :
Intent intent = new Intent(whereYouareActivity.this , mainActivity.class};
startActivity(intent);

when you finish an activity, the activity gets destroyed but not the process. This is an important aspect of Android programming and is important to understand how it's different from other platforms.
And you can finish your activity by adding these lines :-
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
startActivity(new Intent(this, Home.class));
finish();
}

After finish() you can't get context from the activity.
You have to start activity like this:
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
startActivity(new Intent(this, Home.class));
finish();
}

By calling finish() you actively close your current Activity. This prevents your activity from being shown again when the user presses the back button.

Related

How to switch Activities without killing them in Android?

I have this Activity1(Restaurant) which switch to Acticity2(Home)
Intent intent = new Intent(RestaurantActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
My other Activity (Home) has a boolean to check if I have been at Home before
public static boolean visitedHomeAlready = true;
if(!visitedHomeAlready) {
Intent intent = new Intent(HomeActivity.this, RestaurantActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); //
}
My idea is: If I have not visited the home before and switch to Restaurant Activity, I want to "remember" the data in the Home Activity to continue my game after come back home but it always start from the beginning from my home...
I am not sure which flags I have to set or which method to resume that state from where exactly where I switch the activity.
As Amin commented above,the finish(); means that the first activity has finished its job and the app will end it,if you removed it the second activity will start,but the first activity will still be in its place and you can go back to it just by clicking the back button on your phone or virtual device.

build a "fake" stake using startActivity()

I need to build a fake back stack in my application. I am starting an activity using aContext.startActivity(aIntent) and would like to build a stack to allow the user to go to the HomeActivity instead of exiting the application.
Now I know about the TaskStackBuilder but I am not sure on how to implement it when it comes to startActivity method.
This is what I got so far when building the stack but I am not sure how to use it in startActivity method
Intent detailActivity = new Intent(aContext, DetailsActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(aContext);
stackBuilder.addNextIntentWithParentStack(detailActivity);
PendingIntent pendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Why don't you just use onBackPressed
Context context = this;
#Override
public void onBackPressed()
{
Intent intent = new Intent(context,YourActivity.class);
startActivity(intent);
}
You need to start activity without adding to back stack, then override the onBackpressed
//Start activity without adding to history stack
Intent i = new Intent(...); // Your list's Intent
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); // Adds the FLAG_ACTIVITY_NO_HISTORY flag
startActivity(i);
//Override the newly open activity onBackPressed()
#Override
public void onBackPressed()
{
Intent i= new Intent(getApplicationContext(),HomeActivity.class);
startActivity(i);
}
The fix that i found to work without having to use onBackPressed was to add a flag to the intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
For building fake stack use TaskStackBuilder.This will help you to create back stack.
Suppose you activities are 'ActivityA', ActivityB, ActivityC, ActivityD. Now you are in ActivityB and from ActivityB you want launch ActivityD and want a back stack like this:
ActivityA -> ActivityC -> ActivityD
That means back press from ActivityD will go to ActivityC and back press from ActivityC wil go to ActivityA
In ActivityB you can use this
TaskStackBuilder.create(this)
.addParentStack(ActivityA.class)
.addNextIntent(new Intent(this, ActivityA.class))
.addNextIntent(new Intent(this, ActivityC.class))
.addNextIntent(new Intent(this, ActivityD.class))
.startActivities();
To use TaskStackBuilder you min sdk version should be 16 or higher.
For sdk less than 16 you can manually check in onBackPressed and start Activity.
In Activity D:
#Override
public void onBackPressed(){
Bundle extras = getIntent().getExtras();
if (extras.containsKey("FROM_ACTIVITY_B_FOR_STACK")){
// start Activity C
}else{
super.onBackPressed();
}
}
Do this for other activities where you want to add back stack

Opening a new activity crashes my application

I do not know if this bug is in the code that I use to open the activity, or if it lies in the activity itself. This is the code for opening the activity, please tell me if I have an error in this, or if it is something else.
public void openGallery(View view){
Intent intent = new Intent(this, PhotoGallery.class);
startActivity(intent);
}

Intent not working always as expected

There are two flows in my app.
Flow 1:
Splash Screen --> Activity A --> Activity B --> Activity C --> Activity A
Flow 2:
Splash Screen --> Activity C --> Activity A
To explain the above; in Flow 1 a user sees the splash screen, goes to home screen, goes to login screen and on successful login, goes to main screen. If the user hits logout from the main screen he is redirected to the home screen.
In Flow 2 the user is an old user, he sees the splash screen and directly sees the main screen. If he logs out he should be directed to the home screen.
The problem which I am facing is that, in Flow 1, everything is working as expected. But in Flow 2, after logout (Activity C), the home screen (Activity A) is not opening. The intent is not working.
On click logout:
#Override
public void onClick(View v) {
((StudyStoryMain)getActivity()).logoutUser();
}
The Method:
public void logoutUser() {
//Problem: the intent is getting called in case the user creates an account. But, if the user is already an existing user, the intent is nit working
ParseUser.getCurrentUser();
ParseUser.logOut();
Intent i = new Intent(StudyStoryMain.this, HomeActivity.class);
//logic to fix logout
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
}
Why is the Intent not working in both the flows?
Try this way :
Intent intent = new Intent(StudyStoryMain.this, HomeActivity.Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
Before finishing current Activity start another Activity :
startActivity(i);
finish();
As in logoutUser() method you are calling finish() before startActivity() which is wrong as startActivity(intent) get never call.
So just change sequence to
startActivity(intent)
finish()
#Override
public void onClick(View v) {
((StudyStoryMain)getActivity()).logoutUser();
finish();
}
The Method:
public void logoutUser() {
ParseUser.getCurrentUser();
ParseUser.logOut();
Intent i = new Intent(StudyStoryMain.this, HomeActivity.class);
startActivity(i);
}
Try this way, hope it solves your problem.

How to restart activity using intent?

I am making an android app which plays mp3 files. I am launching the mp3 playing activity from within another activity using intent:
Intent intent=new Intent(ListViewA.this,mp3player.class);
intent.putExtra("feed",gh);
i.setFlags(0);
i.setPackage(null);
//intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Now when the user selects another song from a list, I want to close the previous instance of the mp3player activity and start a new one using the intent code (above). How do I go about doing that? Thanks.
Instead of startActivity use startActivityForResult(intent, PLAY_SONG) then you can call finishActivity(PLAY_SONG) where PLAY_SONG is a constant number.
class member
private static final int PLAY_SONG
And then
finishActivity(PLAY_SONG);
Intent intent=new Intent(ListViewA.this,mp3player.class);
intent.putExtra("feed",gh);
i.setFlags(0);
i.setPackage(null);
startActivityForResult(intent, PLAY_SONG)
Hey its very simple Instead of calling Intent from Mp3Playeractivity call finish() when you are pressing the back button by implementing
#Override
public void onBackPressed(){
finish();
}
which will cause close your MpeplayerActivity

Categories