return from Dialog Activity to MainActivity - android

I have this issue I start an Activity as a Dialog with attribute android:theme="#android:style/Theme.Dialog" so far so good, when I run this Activity I want the user to fill some EditTexts and then press a button where a background process will be started, now I use this.setFinishOnTouchOutside(false); to pervent the user from clicking outside and finish() being called, my problem is that I don't get how to finish() specifically this DialogLikeActivity, when i call finish() after the background process is started the application is close and i want to return to the MainActivity or the Activity that started the DialogLikeActivity (the MainActivity still visible after the startActivity or startActivityForResult() is called for the DialogLikeActivity, here is what i got:
Code From MainActivity:
Intent intent = new Intent(this, DialogLikeActivity.class);
startActivityForResult(intent,0);
and the button code in DialogLikeActivity:
public void saveClient(View view){
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
}
I guess maybe my problem is that MainActivity is not a parent of DialogLikeActivity, in that case it should be? how to make DialogLikeActivity child of MainActivity and if I achieve that would the call for finish() in DialogLikeActivity finish the Activity itself and not the app?
Thanks in advance and sorry for my english.
EDIT:
this is the Activity being displayed as a Dialog above the MainActivity

It sounds like DialogFragment might better suit your needs here. It has lifecycle methods similar to an Activity and can run background tasks within itself, but is actually managed by the activity it is attached to. There are several types you can use.
Check out the Google documentation on it HERE.

Related

Check if a previous activity exists

I have an activity. After checking some stuff, I want to go back to previous activity if a previous exists, and if not I want to start a SpecificActivity. How do i do it?
Edited:
Seems like no one is understanding what I meant, so let me rephrase. Lets say I am in Activity A. I don't know if A is the only activity in the stack. If there are other activities, then I want to finish A and pop the activity right below A in the stack into the foreground. If A is the only activity in the stack, I want to start some activity Z. How do i do it?
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can simply override the onBackpress()
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(this, Destination_Activity.class));
finish();
}
call the next activity like.
Intent intent = new Intent(this,Your_Next_Activity.class);
startActivity(intent);
then it will call your another activity and your current activity will be on background if you will use finish() after calling next activity it will finish your current activity so don't use finish() after calling your next activity in this scenario.
After that when you press back button it will automatically finish current activity.

How to kill a particular activity?

When my android app is started, main activity is launched. It displays a full screen image for 5 seconds, and then it jumps to another activity using intent. What i want is to kill the main activity, so that when user presses the back button of navigation bar, instead of opening main activity, the app gets closed.
One more thing:- i don't want to keep on destroying previous activities. I just want to kill that one activity(namely main activity), just after the intent is sent to new activity, Because i will be adding more activities.
We can say that my true purpose is destruction of main activity, and making the next activity(out of all other activities) as a activity through which the app can be leaved using back button of navigation bar.
I am not able to properly explain my problem in words, but please try to figure out my problem what what all i have mentioned.
In your MainAcitivity ,call the second activity like this:
Intent intent=new Intent(this,<your second activity.class>;
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
I would add
android:noHistory="true"
to the AndroidManifest.xml, specifically in the MainActivity definition
To kill Activity you have to use method finish();
In MainActivity in code, where you are starting next activity add finish();:
Intent i = new Intent(MainActivity.this, NextActivity.class);
startActivity(i);
finish();
What you're trying to achieve is called a splash screen.
In your main activity start another activity and if the user presses back on another activity, simply call o finish on main activity. Destroying parent activities before child is sort of messy.
I'd recommend googling splash screens through cold app booting.

How to close activity and go back to previous activity in android

I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
The above code was run from the main activity.
Now in my new activity which is called by the main activity, I have a back button.
When I click on this back button I want my new activity to close and it must go back to the original main activity.
I have tried calling super.finish() and just finish() (from the new activity) but this then closes my entire application (including my main activity).
How can I just close the activity that is currently in focus, and then return to the main activity?
EDITED
The fact that my phone's back button also closes my entire app, leads me to think that i have started up the second activity incorrectly?
OK I have been looking,
I created a Settings Activity that uses the same manifest code and the same code to Start the activity.
For the settings Activity when I push the back button, it returns to the Main activity.
With the activity mentioned above in the main question it simply exits my entire app.
So the problem doesn't seem to be with the code to finish the activity but the activity itself.
I think you are calling finish() method in MainActivity before starting SettingsActivity.
The scenario which you have described will occur in following two ways:
EITHER
You have set android:noHistory = "true" for MainActivity inside AndroidManifest.xml which causes MainActivity to finish automatically on pressing the back key.
OR
Before switching to your 'SettingsActivity', you have called finish() in your MainActivity, which kills it. When you press back button,since no other activity is preset in stack to pop, it goes back to main screen.
You can go back to the previous activity by just calling finish() in the activity you are on. Note any code after the finish() call will be run - you can just do a return after calling finish() to fix this.
If you want to return results to activity one then when starting activity two you need:
startActivityForResults(myIntent, MY_REQUEST_CODE);
Inside your called activity you can then get the Intent from the onCreate() parameter or used
getIntent();
To set return a result to activity one then in activity two do
setResult(Activity.RESULT_OK, MyIntentToReturn);
If you have no intent to return then just say
setResult(Activity.RESULT_OK);
If the the activity has bad results you can use Activity.RESULT_CANCELED (this is used by default). Then in activity one you do
onActivityResult(int requestCode, int resultCode, Intent data) {
// Handle the logic for the requestCode, resultCode and data returned...
}
To finish activity two use the same methods with finish() as described above with your results already set.
if you use fragment u should use
getActivity().onBackPressed();
if you use single activity u can use
finish();
When you click your button you can have it call:
super.onBackPressed();
Button edit = (Button) view.findViewById(R.id.yourButton);
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(this, YourMainActivity.class);
startActivity(intent);
finish();
}
});
try this code instead of finish:
onBackPressed();
I believe your second activity is probably not linked to your main activity as a child activity. Check your AndroidManifest.xml file and see if the <activity> entry for your child activity includes a android:parentActivityName attribute. It should look something like this:
<?xml ...?>
...
<activity
android:name=".MainActivity"
...>
</activity>
<activity
android:name=".ChildActivity"
android:parentActivityName=".MainActivity"
...>
</activity>
...
This closes the entire application:
this.finish();
You are making this too hard. If I understand what you are trying to do correctly, the built-in 'back' button and Android itself will do all the work for you: http://developer.android.com/guide/components/tasks-and-back-stack.html
Also, implementing a custom "back" button violates Core App Quality Guideline UX-N1: http://developer.android.com/distribute/googleplay/quality/core.html
I don't know if this is even usefull or not but I was strugling with the same problem and I found a pretty easy way, with only a global boolean variable and onResume() action. In my case, my Activity C if clicked in a specific button it should trigger the finish() of Activity B!
Activity_A -> Activity_B -> Activity_C
Activity_A (opens normally Activity_B)
Activity_B (on some button click opens Activity_C):
// Global:
boolean its_detail = false;
// -------
SharedPreferences prefs = getApplicationContext().getSharedPreferences("sharedpreferences", 0);
boolean v = prefs.getBoolean("select_client", false);
its_detail = v;
startActivity(C);
#Override
public void onResume(){
super.onResume();
if(its_detail == true){
finish();
}
}
So, whenever I click the button on Activity C it would do the "onResume()" function of Activity B and go back to Activity A.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if ( id == android.R.id.home ) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Try this it works both on toolbar back button as hardware back button.
Finish closes the whole application, this is is something i hate in Android development not finish that is fine but that they do not keep up wit ok syntax they have
startActivity(intent)
Why not
closeActivity(intent)
?
We encountered a very similar situation.
Activity 1 (Opening) -> Activity 2 (Preview) -> Activity 3 (Detail)
Incorrect "on back press" Response
Device back press on Activity 3 will also close Activity 2.
I have checked all answers posted above and none of them worked. Java syntax for transition between Activity 2 and Activity 3 was reviewed to be correct.
Fresh from coding on calling out a 3rd party app. by an Activity. We decided to investigate the configuration angle - eventually enabling us to identify the root cause of the problem.
Scope: Configuration of Activity 2 (caller).
Root Cause:
android:launchMode="singleInstance"
Solution:
android:launchMode="singleTask"
Apparently on this "on back press" issue singleInstance considers invoked Activities in one instance with the calling Activity, whereas singleTask will allow for invoked Activities having their own identity enough for the intended on back press to function to work as it should to.
on onCreate method of your activity
write the following code.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Then override the onOptionsItem selected method of your activity as follows
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
}
return super.onOptionsItemSelected(item);
}
And you are good to go.
Just don't call finish() on your MainActivity then this eliminates the need to Override your onBackPressed() in your SecondActivity unless you are doing other things in that function. If you feel the "need" for this back button then you can simply call finish() on the SecondActivity and that will take you to your MainActivity as long as you haven't called finish() on it
it may be possible you are calling finish(); in the click button event so the main activity is closed just after you clicking the button and when you are coming back from next activity the application is exit because main activity is already closed and there is no active activity.
You have to use this in your MainActivity
Intent intent = new Intent(context , yourActivity);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(intent);
The flag will start multiple tasks that will keep your MainActivity, when you call finish it will kill the other activity and get you back to the MainActivity
In case none of the above answers helped, I think this might help someone.
I was also having the same problem while pressing the built-in back button or my custom back button, the app closes without returning to the previous activity.
I was calling the second activity from the first activity's toolbar.
But in the starter activity I was calling this:
case android.R.id.home:
if (isActionMode) {
clearSelectingToolbar();
adapter.notifyDataSetChanged();
} else {
onBackPressed(); // This was making the activity to finish
}
break;
And this code to start the activity
case R.id.settings:
context.startActivity(new Intent(ShowAllDirectoriesActivity.this, SettingsActivity.class));
After removing 'case android.R.id.home' part, my activity was able to perform in a normal flow i.e getting back to the previous activity.
So check it if you are also using the same thing!
{ getApplicationContext.finish(); }
Try this method..

From within an android activity test, how do I finish another activity that my main activity started?

I am writing an activity test for an activity we wrote with 3 buttons. 2 of these buttons start other activities.
I can write a test that simulates a button push and then checks if the desired activity is running, but I can't move back from that second activity. The second activity stays at the front and prevents the other tests, that assume the first activity is running, from working properly. They just kind of freeze.
I have a reference to the first activity, but it is the second activity I need to I guess call finish() on. Is there a way to do this?
EDIT: I added some actual source code illustrating my problem in this gist: https://gist.github.com/3076103
It is specifically about testing activities. In the production code everything is fine.
You should probably use http://developer.android.com/reference/android/app/Instrumentation.ActivityMonitor.html to get a reference of the second activity or you can block the second activity from being launched(Still you are guranteed that the call to start the second activity infact had reached till the framework).
You need a way for your activities to communicate with one another, so that one activity can tell the other to finish. There are several ways you can accomplish this. One method is to create a service within my application; my "second" activities would connect to this service to register a way to receive messages, and my primary activity would connect in order to provide them.
In Activity1 add the following to start Activity2
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
In Activity2 add the following to start Activity1 and finish Activity2
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
For more details: http://www.warriorpoint.com/blog/2009/05/24/android-how-to-switch-between-activities/
If you start the activity using startActivityForResult, then you can close the activity from parent using finishActivity(int requestCode).
To start the activity :
startActivityForResult(new Intent(...), 123123 /*requestCode*/);
And when you want to finish that activity (from caller), use :
finishActivity(123123 /*requestCode*/)
Also there is a way to find, whether child activity is finished or not. But you can track this only when child activity calls finish() for self. To receive the child finish request from the child, you need to override the finishFromChild() method in parent activity.

Android : Finish Activity from other Activity

I am having application in which i am using menu , on tap on menu item i am redirecting to specified activity.
I want to make sure that when i am redirected to another menu item my current all activity should be finished to reduce the stackflow of the activity and better performance.
So when i tap on back from my tapped activity from the selected menu activity i should be redirected to another activity and finish current activity.
So i am wondering is there any way by which i can finish another activity from my current activity. Or should i override the OnKeyDown Method..
Any help on this
Thanks in advance
You can do:
Intent intent = new Intent(....) ; // intent to launch the new activity
// fire intent
finish(); // finish current activity
Intent i=new Intent (currentclass.this , nextclass.class);
startActivity(i);
finish();
Use finish() as shown above ,and here we are redirectin to nextclass.class so all activity of currentclass should be finished to reduce the stack over flow ..
and mind it finish() calls onDestroy() .and onDestroy() is executed...and destroy current activity(in my case currentclass)..
also mind it. onDestroy() isn't a destructor (as in c++) .It doesn't actually destroy the object of current activity... That's it....

Categories

Resources