Suppose there are 3 Activities namely, activity A, activity B, activity C respectively.
If I call activity C on click of activity A then on back-press I get back to activity A.
But if I call activity C on click of activity B then on back-press I get back to activity B.
Is that possible the same activity (activity C) has two different parent Activity back events, which work differently when its called ?
Override onBackPressed() method and you can go to any activity on backpress.
#Override
public void onBackPressed() {
super.onBackPressed();
//If you want to go to new activity
Intent myIntent = new Intent(YourCurrentActivity.this, YourNextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
YourCurrentActivity.this.startActivity(myIntent);
}
Aan if you want to go to previous activity on back press:
#Override
public void onBackPressed() {
super.onBackPressed();
//If you want to go to previous activity
YourCurrentActivity.this.finish();
}
In case of toolbarNavigation you can use like this in your OnCreateView():
Toolbar toolbar=(Toolbar) findViewById(R.id.my_toolbar);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_material);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
Intent myIntent = new Intent(YourCurrentActivity.this, YourNextActivity.class);
myIntent.putExtra("key", value); //Optional parameters
YourCurrentActivity.this.startActivity(myIntent);
}
});
Related
I am working an android app that has one activity and multiple fragment so I added a lockout feature which locks out the user if there is no interaction in ten minutes. I did this by creating an activity that extends AppCompatActivity and navigates to lockScreenActivity if there is no interaction with the app. Now after the app is unlocked from a web socket I want to navigate back to the activity retaining the current fragment before navigation. How should I go about it.
Searches in stack overflow suggests that I do:
startActivity(new Intent(this, MainActivity.class));
This does not work for me, calling finish on LockScreenActivity did not work too.
Thanks.
If you are going from a Fragment to your MainActivity you should do this
Intent intent = new Intent(getActivity(), mFragmentFavorite.class);
startActivity(intent);
But if you are going from Activity to Fragment , do this
Intent intent = new Intent(this, mFragmentFavorite.class);
startActivity(intent);
if you are doing a finish() going to lockActivity and doing another finish inside lockActivity , there is no activityes in the stack to inflate, maybe thats why is not working for you
If you want to go back from Activity to Fragment.
This is very simple just override onBackPressed() in your activity and call onBackPressed where you want. See my code bellow this will more clear your concept.
#Override
public void onBackPressed() {
super.onBackPressed();
}
//now call onBackPressed to navigate from activity to fragment
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
In my android application suppose several activities are there
if using intent I go to other activities like this
[Activity A]->[activity B]->[Activity C]->[Activity-D]->[Activity N]
and now when am on activity N when I pressed button then I want to go to Activity B and want to destroy Activity C And Activity D but Activity A should not destroy. I also searched in various posts but I didn't get exactly the same solution.
Any help will be appriciated
In ActivityN, to return to ActivityB use this:
Intent intent = new Intent(this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
Using the flag CLEAR_TOP will finish all activities in the stack that are on top of ActivityB. In the example you gave, this will finish ActivityN, ActivityD and ActivityC. Using the flag SINGLE_TOP will make sure that this will return control to the existing instance of ActivityB (ie: it won't create a new instance of ActivityB).
In Your Activity C do like this
public static ActivityC instance = null;
public class ActivityC extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
}
}
And in your Activity D do like this
public static ActivityD instance = null;
public class ActivityD extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = this;
}
}
Finally in your Activity N. Do Something like this
public class ActivityN extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button yourButton= (Button) findViewById(R.id.yourButton);
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ActivityC.instance.finish();
Activityd.instance.finish();
finish();
}
});
}
}
Here's my approach.
From Activity A, don't just start the Activity B, call startActivityForResult() method. Do this for all subsequent calls.
Now, when you press the button from Activity N, set the result for a custom value and call the finish() method for Activity N. Now you should hit the onActivityResult method on your Activity D. Now you can check whether the result was you pressing the button. Depending on your result, keep on setting the result and subsequently calling finish() on each Activity.
This should technically work.
Try this code:
//Activity A
Intent i = new Intent(getApplicationContext,ActvityB.class);
startActivity(i);
//Activity B
Intent i = new Intent(getApplicationContext,ActvityC.class);
startActivity(i);
//Activity C
Intent i = new Intent(getApplicationContext,ActvityC.class);
startActivity(i);
finish();
// finish here actvity which you want to finish
//Try this second 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();
}
EDIT : Use startActivityForResult() instead of startActivity()
So depending on the result you can change the behavour.
Say for example When you wanted to go to ActivityB just return some flag in the INTENT. When it will be caught in Activity D and C in onActivityResult(), finish them and you will be finally on B.
Flag Intent.FLAG_ACTIVITY_CLEAR_TOP may solve your problem:
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
You can start ActivityC, ActivityD, ActivityN with the same request code passed to startForResult(requestCode)
And then at ActivityN, use finishActivity(int requestCode).
Documentation for finishActivity(int requestCode)
Force finish another activity that you had previously started with startActivityForResult.
Params:
requestCode – The request code of the activity that you had given to startActivityForResult().
If there are multiple activities started with this request code, they will all be finished.
I'm starting a Fragment activity with 2 fragments from a menu item of an activity.
How can I return back to this activity by pressing the back key? With normal activities this happens by default but with fragment the first activity is destroyed.
You must use an Intent when the method onBackPressed() is calling, for example :
#Override
public void onBackPressed() {
Intent i = new Intent(getActivity(), yourFirstActivity.class);
startActivity(i);
}
getActivity() is optimizing for Fragment.
Hope this help
When you press the back button while in an activity, by default, does the application not go back to the activity that called it? I am calling an activity in my application(call it Activity B), from Activity A, but when I hit the back button while in Activity B, I am taken back to the main page of the application.
So I guess in general, does pushing the back button on your phone take you to the calling activity?
Calling activity B from within an inner class of activity A:
class HeadlineButtonListener implements OnClickListener {
private Story story;
public HeadlineButtonListener(Story story) {
this.story = story;
}
#Override
public void onClick(View v) {
Intent myIntent = new Intent(HeadlineBoard.this, StoryView.class);
myIntent.putExtra(Constants.STORY_EXTRA, story);
HeadlineBoard.this.startActivity(myIntent);
finish();
}
}
You call finish() on first activity after firing the next activity, this will cause it to be removed from the activity stack, just remove the call to finish():
#Override
public void onClick(View v) {
Intent myIntent = new Intent(HeadlineBoard.this, StoryView.class);
myIntent.putExtra(Constants.STORY_EXTRA, story);
startActivity(myIntent);
}
I have 2 activities.
MyMain
MySecond I call MySecond activity in MyMain activity. And what i want, that when pressing back button in MySecond activity, it doesn't return to MyMain activity, but return to the screen where MyMain activity was called. I found a way to ovverride it, so it just opens home screen by this:
#Override
public void onBackPressed() {
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
return;
}
You may want to try this:
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
That will move not only the current activity but all activities in your task (which would be your app) to the back of the activity stack. It should then return you to wherever you were before your MyMain activity started.