How to implement the following? - android

Given:
6 activities. (A, B, C, D, E, F)
Each activity consists of several edittexts or a camera implementation and a next button to goto next activity (The activity which is going to be opened depends on the value entered by the user).
If edittexts are displayable or not comes from the server.
Incase there are no edittexts on the layout then only next button is shown.
Flow of the application: A->B->C(->D)->E->F
The Activity D opens only when a certain condition is met in activity C.
Todo:
Incase, The activity contains no edittext and only next button i should be able to skip this activity.
If the flow of my application is like: A->B->C->E->F Then when i press hard back the flow should be F->E->C->B->A
If the flow of my application is like: A->B->C->D->E->F Then when i press hard back the flow should be F->E->C->B->A
If the flow of my application is like: A->C->D->E->F Here we skipped B because there are no views in Activity B, When i press hard back the flow should be F->E->C->A
what i did:
To skip the activity when no fields are available inside onCreate(), I am checking if any field is displayable if no then i add the activity name in a stack if it is not present in it and then open the next activity.
// Block for skipping this screen
if (skipScreen) {
Intent i = new Intent(B.this, C.class);
startActivity(i);
finish();
} else {
if (!Constants.st.contains(B.class)) {
Constants.st.push(B.class);
}
}
And when i press back from an activity i pop() the name of that activity from the stack and peek() at the top of the stack and jump to that activity.
public void onBackPressed() {
super.onBackPressed();
Constants.st.pop();
Intent i = null;
if (Constants.st.isEmpty()) {
i = new Intent(B.this, A.class);
} else {
Class<Activity> jumpTo = Constants.st.peek();
i = new Intent(B.this, jumpTo);
}
startActivity(i);
finish();
}

I think the statePattern will be very helpfull. Here is a short tutorial.

Related

Clear upto a specific activity from the stack history

Suppose I have activities named A, B, C, D. Now, consider A has been launched as the root activity and B has been launched from A and C has been launched from B and D has been launched from C.
Now I have a button named "Success" in the activity D. If suppose, I press the button "Success" in the activity D, then the activity C should be removed from the history stack and go to activity B, but on Pressing back key from the activity D should display activity C instead of B and Clear D from stack. Please can anyone help me to resolve this problem?
Add this Code in your Activity_D for Button click
btnSuccess.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(Activity_D.this, Activity_B.class));
finishAffinity();
}
});
Also add this in your Activity_D to Handle Back Button Pressed.
#Override
public void onBackPressed() {
finish();
super.onBackPressed();
}
From Activity_C to Activity_D
startActivity(new Intent(Activity_C.this, Activity_D.class));
Don't call finish() here.
When you click on Success button, you can use the following line of code to open your B activity:
Intent d_intent = new Intent(D.this, B.class);
d_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
d_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(d_intent);
finish();
Here Activity B will be started from the back stack rather than a new instance because of Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK clears the stack and makes it the top one. So when we press the back button the whole application will be terminated. And on Back press you need to simply call
finish();
Hope it will help you out.

Conditionally clear activity backstack

I am working on an app on which you can share images from your local phone's gallery app. Whenever a photo is shared through the gallery app, an Activity A opens up in my app.
The photo sharing follows some steps in which it goes from Activity A to Activity B and the final step in B opens the Main Activity.
What I want, pressing back from B should open A. Pressing the final button in B should clear both A and B from backstack, so that if you press back on Main Activity, the app should close.
What I did :
Initially, I was opening Main Activity with this flag intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
But, this started a new task and so even when I used to close my app and go to the phone's gallery app, it would show A instead of the photo on that app, when you pressed back, then you could see the actual image in that gallery app.
I had specified android:noHistory="true" for A and B and started the intent to Main Activity without the new task flag. This solved the new task problem in the sense that when I close my app and switched back to the gallery app, it showed the actual image not my activity A. But the issue here is, I can't go back to A from B, also if the app is minimised, then also the activity is gone.
Create a singleton class :
Stack<Activity> stack;
public void add(Activity activity){
stack.add(activity).
}
public void remove(){
Activity activity = stack.lastElement();
if(activity != null) {
stack.remove(activity).
activity.finish();
}
}
public void removeAll() {
for (Activity activity : stack) {
if (activity != null)
activity.finish();
}
stack.clear();
}
call add() in your activities' onCreate()
call remove will finish current activity
call removeAll will finish all activities added into the stack
Intent l = new Intent(getApplicationContext(), MainActivity.class);
l.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
l.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
l.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(l);
use this on finish button in avtivity B.

How to go previous Activity by using back button

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();
}

Android: How to go back previous Activity

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()

Customizing Back Stack in Android Activities

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);
}

Categories

Resources