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();
}
});
Related
I have the first activity and a button on it , when I click the button , I go to the second activity. And when I moved / am on the second activity I would like the first activity not to be destroyed but to pause(FirstActivity.Pause). Tell me if there are ways to implement this ?
I want to implement this so that the data from the first activity is not deleted.
Thanks!
Your activity is supposed to pause.
You should add this line of code to your on create method:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondaryActivity.class));
}
});
Your Main activity is going to be paused whilst you are on your secondaryActivity.
its the defualt behavior to pause first activty you didn't call finish() on it .
your Intent should be like this
Intent i= new Intent(context, secondActivity.class);
startActivity(i);
please share your code to find issue .
I want to navigate to another screen when user touch back button. I found a method for this.
#Override
public void onBackPressed() {
// do something on back.
return;
}
I'm navigating from a fragment.But it is not clear that where I have to use this and how. Please help.
You'll need to add that method to the Activity class of the screen you're navigating from.
Then, in that method, add your intent code to move to your second Activity (let's call that Activity2 below) using startActivity
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.setClass(this, Activity2.class);
startActivity(intent);
}
Just override the method onBackPressed and navigate to the screen you want to via intents or whichever way you prefer.
#Override
public void onBackPressed() {
getFragmentManager().beginTransaction.replace(thisfragment,new Fragment).commit();
}
The above code will replace your current fragment with the new fragment to which you want to navigate.
I have created two activity i.e Activity A & Activity B ,if i clicked Next Button i.e in Activity A going to Activity B properly But when i click on back button i want to go from Activity B to Activity A and page swipe from left side to right side and on click next page swipe right to left,
here is my code
public void onBackPressed() {
Intent intent = new Intent(ActivityB.this, Activity.class);
startActivity(intent);
finish();
Just Remove finish() from Activity.
Because when you go to second activity and finish first activity, there is no any activity and stack.
So If you click back button from second activity, application will be finish if there is no Activity in stack.
You should use this Approach.
Ex.
In Activity.java
Intent first = new Intent(Activity.this,ActivityB.class);
startAcivity(first);
// Don't use finish() here.
In ActivityB.Java
Just click on built in back button.
or If you want to use your own back button.
Use finish(); in button click event.
You can use only onBackPressed();
public void onBackPressed() {
super.onBackPressed();
}
Android Overriding onBackPressed()
How to go previous Activity by using back button
No need to put an intent and start a new activity that would take you to previous activity.
Just call 'finish()'
It would go back to previous activity as Android activities are stored in the activity stack
If you have other activities that are present in between the activites say if android stack is filled with Activity A>Activity C>Activity B,If you want to go to Activity A on finish of Activityy B then you have to set an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP
Just use finish() no need for intent as A is already in stack and when you finish B, A will come to surface
public void onBackPressed() {
finish();
}
Read this to learn more about android activity stack.
public void onBackPressed() {
// TODO Auto-generated method stub
finish();
super.onBackPressed();
}
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'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