I am trying to finish one activity from another.
For that purpose I am having only the component name of that activity.
How can i finish that ?
1.Make your activity A in manifest file: launchMode = "singleInstance"
2.When the user clicks new, do FirstActivity.fa.finish(); and call the new Intent.
3.When the user clicks modify, call the new Intent or simply finish activity B.
FIRST WAY
In your first activity, declare one Activity object like this,
public static Activity fa;
onCreate()
{
fa = this;
}
now use that object in another Activity to finish first-activity like this,
onCreate()
{
FirstActivity.fa.finish();
}
SECOND WAY
While calling your activity FirstActivity which you want to finish as soon as you move on, You can add flag while calling FirstActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
But using this flag the activity will get finished evenif you want it not to. and sometime onBack if you want to show the FirstActivity you will have to call it using intent.
You can do it in very simple way.
First create a static instance of your activity e.g. MainActivity, whom you want to finish like,
public static MainActivity act=MainActivity.this;
and now in another actvity e.g. MainActivity2 just call this line,
MainActivity.act.finish();
Try extending that activity and override the finish method
public class ma extends MainActivity{
#Override
public void finish()
{
super.finish();
}
}
You want to exit application after log out.
that time to user this
i.addCategory(Intent.CATEGORY_HOME);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
or try to another way like
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
moveTaskToBack(true);
finish();
}
Related
I want to check some condtiton before the main activity starts and based on test result i have to either start a new activity or continue the same activity. How to do this?
you can check it in your OnCreate() method. It is called when your activity start.
onCreate(...){
....
if(want this){
//continue;
}else{
// start new activity
}
}
The default Activity to start is set in the manifest, so a better approach to your problem would be to use fragments. Keep in mind that fragments are faster/lighter, so instead of using Application as a "decision" class to start activities (bad practice), use your main activity. In your onCreate() method, check for your condition and attach the needed fragment.
I am using java annotation to handle this case:
Create the Annoation class
Create CustomContext.java with a startActivity method in it
Create the Interceptor.java
Create a class which implement Interceptor like this (named DemoInterceptor.java here)
Declare a static variable of DemoInterceptor in your activity. and the demo Activity should like this then, start the activity via the custom startActivity in step #2
enjoy and let me know if you have any further question.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(startNewActivity) {
// TODO Fire off intent to start new activity
finish(); // Closes the current activity
return;
}
// TODO Code for current activity.
}
startNewActivity is a boolean indicating whether to launch a new activity or not. It should be assigned a value depending on your condition.
I have four activities
ListActivity
Itemdetail_Activity
record1_Act
record2_act.
I can go from ListActivity -> record1_Act and from ListActivity <-record1_Act properly but when I want to go from Itemdetail_Activity -> record2_act and return back from Itemdetail_Activity <- record2_act it always goes record2_act to ListActivity.
I even used i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); but it does not work. How to solve this issues?
Here is my code
Intent i = new Intent(record2_act.this, Itemdetail_Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
Intent i = new Intent(record1_Act.this, ListActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
After starting activity remove finish() call because it will clear that activity from stack and i see you have used finish in your code. also you do not need to set these flags.
In activity 4, implement onBackPressed() as,
#Override
public void onBackPressed()
{
Intent i = new Intent(4.this, 3.class);
startActivity(i);
}
call this method if you have any back button. And if you want to link the backpress for every activity you need to implement onBackPressed() method in every activity.
you can call onBackPressed() if previous activity not finished also you can use :
this.finish();
// Or
Activity currentActivity = this;
currentActivity.finish();
and if previous you finished activity you have to track user in activities and make list of activities, then move to :
activitiList.get(activityList.size() - 2);
// activityList.size() - 1 is last activity and activityList.size() - 2 is previous activity
Do check if ItemDetailsActivity is NOT calling finish()
I am developing an android app where I want to explicitly destroy an activity once it has stopped.activity.onFinish() not serving the purpose as it returns to the previous activity.
Example of my situation:
When I move from MainActivity to another activity,say B, I want to destroy the mainactivity. So that on pressing back from B now, I will move out of the application.Is that possible?
MainActivity code:
new Helper(this).startVerification();
In Helper class:
Helper{
Context myContext;
Helper(Context c){
this.myContext=c;
}
//other methods
public void startVerification(){
prepareForVerificationStart();
Intent i=new Intent(myContext,VerificationNumberEntryActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myContext.startActivity(i);
Activity a=(Activity) myContext;
a.finish();
}
}
When you call startActivity() to launch B, also call finish() to get rid of MainActivity.
This is my code for achieving exactly what you're asking.
I'm using it in my own app, after the user has logged in, to destroy the login activity so the user cant press back to return to the login activity.
Activity tmpAct = getActivity();
Intent i = new Intent(tmpAct, MainActivity.class);
startActivity(i);
tmpAct.finish();
Basically i save the current activity (login activity), then start the new one, and then do .finish() on the saved activity.
How to switch layouts? First, I have a class Main where is onCreate (setContentView(R.layout.main);) and then I call, another class with command:
setContentView(secondClass);
In this class, I draw with Canvas and this work just fine. I also create button to go back in first "class" (R.layout.main), but I don't know how to do it.
Now my program is basic a graph shower. In first class you type your function and them second class draw it. But how to go back in first class to type another function. This "back" button or arrow witch every Android phone have, send me out of program not back on insert part.
In secondClass I can't create onCreate method, but I also tried the following and they didn't work:
Intent abc = new Intent("bla.bla.bla.FIRSTCLASS");
startActivity(abc);
and
Intent abc = new Intent(SecondClass.this,FirstClass.class);
startActivity(greNaPrvoOkno);
If you want to use a custom view (as I understood, you are extending the View class), you can do it in the following way;
Consider you are showing the second class from your Main activity like this;
setContentView(new SecondClass(getApplicationContext(), MainActivity.this));
And you Second class is this (suppose);
// I am using onClickListener to go back to main view. You do whatever you like.
public class SecondClass extends View implements OnClickListener {
// This is needed to switch back to the parent activity
private Activity mParentActivity = null;
public SecondClass(Context context, Activity parentActivity) {
super(context);
mParentActivity = parentActivity;
setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Set the Main view back here.
mParentActivity.setContentView(R.layout.main);
}
}
Disclaimer: This code will do what you have asked for, but may cause other problems.
As advised by #Mudassir, you should use two different activities for two screens. It will give you better control, and your code will be easy to understand and maintain.
On the Onclick event of the button you have to write finish(); that's it..
Both of your classes are Activities yes? IF so then in your second activity you will simply call finish() and your activity will close revealing your first activity again.
When I have used multiple intents in my android application, I have created a new activity through:
Intent abc = new Intent(this, SecondClass.class);
startActivity(abc);
When the button is pressed in your second class, I would then either call finish(); on the class, or create a new intent like so:
Intent abc = new Intent(this, FirstClass.class);
startActivity(abc);
However, this method has the disadvantage that if a user wanted to use the back button, they may have to scroll through many layers of activities.
You should create another activity for your second class but not just set the main activity to a new view setContentView(secondClass).
For an easier modification, You could try to set the view back to setContentView(R.layout.main) first.
You still need to configure the widgets(e.g. TextView) on it when you set it back.
You don't have to startActivity again to go back.
Just call finish() in your second activity when you want to finish the current activity and go back:
e.g. When user press the back button in your second activity
mButtonBack.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
}
I have a fragment in an activity that I am using as a navigation drawer. It contains buttons that when clicked start new activities (startActivity from a fragment simply calls startActivity on the current activity).
For the life of me I can't seem to figure out how I would finish the current activity after starting a new one.
I am looking to achieve something like this in the fragment:
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view == mButtonShows) {
Intent intent = new Intent(view.getContext(), MyNewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
}
But it seems Fragment.class does not implement finish() (like it implements startActivity(...)).
I would like the activity backstack cleared when they launch the 2nd activity. (so pressing back from the new activity would technically drop them back to the launcher)
When working with fragments, instead of using this or refering to the context, always use getActivity(). You should call
Java
getActivity().finish();
Kotlin
activity.finish()
to finish your activity from fragment.
Well actually...
I wouldn't have the Fragment try to finish the Activity. That places too much authority on the Fragment in my opinion. Instead, I would use the guide here: http://developer.android.com/training/basics/fragments/communicating.html
Have the Fragment define an interface which the Activity must implement. Make a call up to the Activity, then let the Activity decide what to do with the information. If the activity wishes to finish itself, then it can.
As mentioned by Jon F Hancock, this is how a fragment can 'close' the activity by suggesting the activity to close. This makes the fragment portable as is the reason for them. If you use it in a different activity, you might not want to close the activity.
Code below is a snippet from an activity and fragment which has a save and cancel button.
PlayerActivity
public class PlayerActivity extends Activity
implements PlayerInfo.PlayerAddListener {
public void onPlayerCancel() {
// Decide if its suitable to close the activity,
//e.g. is an edit being done in one of the other fragments?
finish();
}
}
PlayerInfoFragment, which contains an interface which the calling activity needs to implement.
public class PlayerInfoFragment extends Fragment {
private PlayerAddListener callback; // implemented in the Activity
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
callback= (PlayerAddListener) activity;
}
public interface PlayerAddListener {
public void onPlayerSave(Player p); // not shown in impl above
public void onPlayerCancel();
}
public void btnCancel(View v) {
callback.onPlayerCancel(); // the activity's implementation
}
}
You should use getActivity() method in order to finish the activity from the fragment.
getActivity().finish();
This does not need assertion, Latest update in fragment in android JetPack
requireActivity().finish();
In Fragment use getActivity.finishAffinity()
getActivity().finishAffinity();
It will remove all the fragment which pushed by the current activity from the Stack with the Activity too...
Every time I use finish to close the fragment, the entire activity closes. According to the docs, fragments should remain as long as the parent activity remains.
Instead, I found that I can change views back the the parent activity by using this statement:
setContentView(R.layout.activity_main);
This returns me back to the parent activity.
I hope that this helps someone else who may be looking for this.
Very simple...
1- just grab activity by getActivity() in the fragment
2- then call finish();
So just getActivity().finish(); will finish the parent activity.
Try this. There shouldn't be any warning...
Activity thisActivity = getActivity();
if (thisActivity != null) {
startActivity(new Intent(thisActivity, yourActivity.class)); // if needed
thisActivity.finish();
}
You have two options for Java and Kotlin. However, logic of both ways are same. You should call activity after call finish() method.
Answer for Kotlin,
If your activity cannot be null, use Answer_1. However, if your activity can be null, use Answer_2.
Answer_1: activity!!.finish()
Answer_2: activity?.finish()
Answer for Java,
getActivity().finish();
To finish activity in a Fragment use:
getActivity().finish();
Simple solution:
activity?.finish()
yes Fragment.class does not implement finish()
When working with fragments, instead of using this or refering to the context, always use getActivity(). You should call