How to make intent go back to previous fragment? - android

I have a fragment which opens an activity (let's call it activity1). This activity (activity1) can open another activity (let's call it activity2). And again this activity (activity2) can open another activity (let's call it activity3). I would like to get the following behavior:
When pressing back in activity1 it will go back to the fragment - works.
When pressing back in activity2 it will go to activity1 - works.
When pressing back in activity3 it will go back to the fragment - does not work.
The last one does not work because it goes back to activity1. When moving from activity2 to activity3 I finish activity2. But how can I also finish activity1? I can't use the (Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK) flag like it was suggested in previous topics because I want it to go back to already opened fragment. How can I remove activity1 from the intent stack?

if you want the flexibility to have activity3 to show any activity when it finishes, you need to override onKeyDown and handle KEYCODE_BACK.
in Activity3.java
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
//show fragment or startActivity to show activity with fragment
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Related

finish my acivity and back to fragment when click on back button and don't erase my data in fragment

I have a mainActivity in which I use my fragment. and I have two activities Activity1 and Activity2. first I go to fragment to Activity2, when I click back button in Activity2, I need to back to fragment page .
it means I need to finish my Activity2 and also finish my Activity1 in one click. how can I get this . here is my back press code on Activity2.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
if(fromString.equalsIgnoreCase("Activity2")){
this.finish();
Activity1.finish();
}else if(fromString.equalsIgnoreCase("Activity1"){
finish(); // close this activity and return to preview activity (if there is any)
}
}
return super.onOptionsItemSelected(item);
}
As far as i am able to understand by your question you want to finish activity1 if you are from activity1 and both activity1 and activity2 if you are from activity2. You can achieve this by two ways :
First way : Replace your code by this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle arrow click here
if (item.getItemId() == android.R.id.home) {
if(fromString.equalsIgnoreCase("Activity2")){
Activity1.finish(); // I have change the sequence of lines by that it will finish Activity1 first then Activity2 in which is residing
this.finish();
}else if(fromString.equalsIgnoreCase("Activity1"){
finish(); // close this activity and return to preview activity (if there is any)
}
}
return super.onOptionsItemSelected(item);
}
Second Way : you can clear all your back stack activities by this code and lend to your expected Activity or fragment by this code :
Intent gotoexpectIntent = new Intent(yourActivity.this,yourexpectedActivity.class);
gotoexpectIntent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(gotoexpectIntent );
it's still unclear what you are asking. but to finish multiple activities on back press you can try startactivityforresult and go on from there.

Activity A is not Coming From Activity B

I am starting B Fragment from Fragment A.Now from Fragment B i hit Home Button.Again i open the app and it calls OnResume. Now if i hit back button it exit from the app.What should i do?
Fragment A to Fragment B Activity
Intent find = new Intent(getActivity(),FindActivityMain.class);
find.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
***startActivity(find);***Here i am not finishing the activity.
Fragment B Activity to Fragment B transaction code
Fragment myfindfragment = new FindFragmentMain(FindActivityMain.this,mylistitem,FindActivityMain.this,distance,featuredAD);
getSupportFragmentManager().beginTransaction()
.replace(R.id.blankfindlandingframe, myfindfragment).commitAllowingStateLoss();
}
Fragment A Activity is "SingleInstance" declared in Manifest.In General the Back Navigation works Perfect.But If i do these step ->
1.) Click Home Button
2.) Again long press home button to restore the app
3.) Click back button.Exit from the app.
This 3rd Step should not occur.
Please Help !!
remove find.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
add your code 'addToBackStack(null)'
Fragment myfindfragment = new FindFragmentMain(FindActivityMain.this,mylistitem,FindActivityMain.this,distance,featuredAD);
getSupportFragmentManager().beginTransaction()
.replace(R.id.blankfindlandingframe, myfindfragment).addToBackStack(null).commitAllowingStateLoss();
}
Inside Activity B add this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}

Switching current tab with back button finishes an activity android

I'm working on an app that uses tab groups to switch between activities. I know that the TabActivty is depreciated and using views instead of activities is the better way to go, however due to time constraints I don't have the time to rewrite the app.
So here is what is going on. I have 4 tabs, lets say Tab1, Tab2, Tab3, Tab4.
Let's say Tab1 has ActivityA > ActivityB > ActivityC and I am on ActivityC. When I switch to Tab2 and hit the back button, I would like it to switch back to Tab1.
I'm trying to accomplish this by overriding the onKeyDown method however when I do this, it takes me back to Tab1, then closes the current activity. So where I was on ActivityC, when it switches back to Tab1, ActivityC closes and leaves ActivityB. This is a problem, especially if I am on ActivityA because it will close the app out. Here is the override method I'm using. Any help is appreciated!
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
#SuppressWarnings("deprecation")
TabActivity tabs = (TabActivity) getParent();
tabs.getTabHost().setCurrentTabByTag("tab1");
return false;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed(){
//Do Nothing
}
If you return true instead of false, it should not be handled by anybody else, which should prevent the Activity from closing. Does that help?
Activity.onKeyDown()

Close all child activities down to parent activity

If i have the following
Parent activity > activity 1 > activity 2 > activity 3
Pressing the back button will go back to:
Parent activity > activity 1 > activity 2
Pressing again will go to:
Parent activity > activity 1
is there any way that I can programatically finish activity 1,2 and 3 from activity 3 itself.
In IOS there is a function called popToRootViewControllerAnimated, Which is the type of concept I require in Android
thanks
Launch the child Activities with startActivityForResult() and then in onActivityResult() call finish(). This will bring you back to the Parent Activity.
Alternatively you can also use the Intent flag FLAG_ACTIVITY_CLEAR_TOP while calling startActivity() on Parent Activity from Activity 3.
Add Flag Intent.FLAG_ACTIVITY_CLEAR_TOP while you call startActivity from your last activity. It will clear the activity stack
Check in onResume() of activity2 if you came from activity3 and call finish(), if yes. Same for activity1.
maybe this algorithm can be usefull;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if (keyCode == KeyEvent.KEYCODE_BACK) {
//here you'll check if activity3 alive then join
//else if activity2 alive then join
//else if activity1 alive then join
//else finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}

Why my activities are not performing my overridden back button event?

I want to send back my activity to starting one. Let me explain you.
I am using Main activity having 4 tabs, and each tabs has his own activity. Each activity in has more activities in it.
Problem starts when I click on 1st activity in my 1st tab. The next activity is independent. I create an intent of my next activity. In such case to prevent draw my tab-bar layout in that new activity. I use my existing activity layout, remove all the view in it. Then i put new activity in it.
e.g..
contentViewLayout.removeAllViews();
View view = activityManager.startActivity(id, intent).getDecorView();
contentViewLayout.addView(view, contentViewLayoutParams);
Its done. But problem arise when device BACK button pressed. Because new activity is starting in existing activity. So when back button pressed , then it quite to main activity and application close. I want to do that in such a case "new activity" should be terminate and existing activity must start again. And then if again BACK button pressed then it should terminate main activity.
I override the onKeyDown() down method as follow but it. dose not working properly?
In my new activity.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
childActivity = null;
contentViewLayout.removeAllViews();
mainActivity = new Intent(this,MainActivity.class);
View view = activityManager.startActivity(id, intent).getDecorView();
contentViewLayout.addView(view, contentViewLayoutParams);
return false;
}else{
return super.onKeyDown(keyCode, event);
}
}
But it always quite the application instead of closing child activity
I got it!
Actually the focused activity was the main activity... not the new activity. So all we have to do to set focused to our new activity. So in onCreate() method of new activity I put focused to one any component in new activit e.g..
button.setFocusable(true);
button.requestFocus();
So in such a case new activity onKeyDownButtonWorks
Blockquote
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ){
//My code goes here.
return true;
}
return super.onKeyDown(keyCode, event);
}
Take a look at this solution:
ActivityGroup not handling back key for ListActivity

Categories

Resources