i have 5 layouts and buttons that navigate from one to another live this: a-c, a-b, c-d, d-e.
I only can navigate from a to b anc c. I put the same code to the others but application stops.
Assuming your layouts are Activities; All you need to do is set button click listeners and call corresponding activity in listener block.
For instance; in ActivityA (onCreate() method)
btnStartB = (Button)findViewById(R.id.btnStartB);
btnStartB.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
you will need to do same for btnStartC in ActivityA, btnStartD in ActivityC and btnStartE in ActivityD. Try debugging and figure out where it stops.
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 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();
}
});
I have an activity which holds a fragment called paymentfragment, paymenfragment holds all the cards a person has. Then I have an activity called addcardactivity which is where a user can add cards. There is a button on the paymentfragment which takes the user to the addcardactivity, but when they are done adding their new card I want the user to go back to the paymentfragment and I want the payment fragment to refresh so that it can hold the new card.
The way I am doing, it is not to end the paymentfragment, and once the addcardactivity is done i finish that activity, the only problem is then it does not refresh.
This is the relevant code from the paymentfragment
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AddCardActivity.class);
getActivity().startActivity(intent);
}
Now ideally, under the startactivity, i would run the method refreshtasks();
however that doesn't work because it just runs it too early.
Any ideas?
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();
}
Consider there is one activity, named Activity S.
Then there will be many activities, say 10 activities, named A, B, C, ..., J.
How can I achieve this:
From those A--J activities, when the back button is pressed, always go back to activity S.
Regardless of the ordering or how the activities are created.
For example:
Starting from S, go to B, then D, then G.
In activity G, press back button, and will go back to S.
== EDIT ==
Should I use Activity.finish() method when leaving all A--J activities ?
You could accomplish this in different ways depending on the exact result you desire. You could add the following line to your <activity> tags in your manifest.xml for those Activities
android:noHistory="true"
or use an Intent flag when overrdiing onBackPressed() in each
#Override
public void onBackPressed()
{
Intent i = new Intent(CurrentActivity.this, S.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);`
startActivity(i);
super.onBackPressed();
}
this will clear the other Activities from the stack and return you to S.
If you want to keep the Activities on the stack while returning to S then you can change the flag used
#Override
public void onBackPressed()
{
Intent i = new Intent(CurrentActivity.this, S.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);`
startActivity(i);
super.onBackPressed();
}
The last way will bring S to the front and keep the other Activities on the stack which I don't think is what you want but just another option. You will probably want one of the first two ways.
You have to do like this to achieve, Set a flag that will Clear every activity from stack and pass intent to S.class, like this
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
Intent intent=new Intent(this,S.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}