back to previous activity - android

I try to go to previous Activity (Activity A) but have problem. Command to this get inside Activity B and I wont to go back to B:
A:
if(...)
{
B.staticF();
}
B:
static void staticF()
{
super.onBackPressed();
}
But I can't use super because it's static context.
Of course, I can call
Intent i = new Intent(this, B.class);
startActivity(i);
but I wont to save B look.

Why not just use something like a shared preference to save the state? and then use the intent to go back and in the onCreate method get the preferences and populate any views with the data you wanted to save

Related

How to maintain data of five activities when comes to First activity on click of back in fifth Activity

I am writing a application where I am dealing with 5 activities, let's say A, B, C , D & E.
Activity A invokes B, B invokes C, C invokes D,D invokes E. On each of the activity, I have a button called "next" button and editText to fill name. When user clicks on next button in any of the A,B, C, D,E activities, application should go to next activity screen but when user clicks On back button in E activity it should go on activity A.
Now my question is when i start from activity A after coming from activty E on back press I want to maintain data of editText of all the actvities which filled by user when comes first time ?
Try This:
Have a Shared Preference file. When clicking next you save the fields in your shared preference.
then when loading a activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
et1 = (EditText)findViewById(R.id.editText_Name);
loadSavedPreferences();
}
private void loadSavedPreferences() {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
et1.setText(sharedPreferences.getString("string_et_Name",""));
}
Have your own saving mechanism on the next button click.
Note: Clear your shared preference when clicking finish (if required)
step 1: decalre a boolean flag with int value on MainActivity first suppose:
private boolean flag=200;
step 2: from main activity we are switching to suppose 5 other activities on a button click respectively.
So from main activity we call StartActivityForResult(intent,flag); which indicate that we can come back with the same flag value from any activity.
step 3: now suppose we want data of activity "E" (as per your question) then we send it by sending it through bundle;
like: declare same boolean value of 200 on each activity from which we are traveling
Intent intent=new Intent();
intent.putExtra("firstName",firstName);
intent.putExtra("lastName",lastName);
setResult(flag,intent);
finish();
step 4: get bundle vale on coming activities by onActivityResult() method which is overriden
and check with flag value
like:
if (resultCode==200)
{
String firstName=data.getStringExtra("firstName");
String lastName=data.getStringExtra("lastName");
Intent intent=new Intent();
intent.putExtra("firstName",firstName);
intent.putExtra("lastName",lastName);
setResult(200,intent);
finish();
}
step 5: similarly for all activities;
step 6: finally, get bundle on MainActivity
like:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==200)
{
String firstName=data.getStringExtra("firstName");
String lastName=data.getStringExtra("lastName");
textViewFirstName.setText(firstName);
textViewLastName.setText(lastName);
}
}
I did something very similar to your needs and like you i started with activities but then I found this, which makes all easy, 1 activity with several fragments instead of lots of activities, maybe you are interested in : StepStone
Here the answers to common questions : ans
Then if you want to pass data, i am working with Parceler because i saved my data in custom classes, check my question:
How can i correctly interact between Activities and fragments
If you have any doubts just say it.
Hope this helps!

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

Return back to previous previous activity

Here is what the structure looks like:
Activity 1/2/3 -> Activity 4 -> Activity 5
On Act4, if a boolean variable is true, it jumps straight to Act5.
My problem is that when I press back on Act5, i want to get back to the activity which called Act4. But what happens is that it jumps back to Act5. If i use intent on back press, I i wouldn't know which activity called Act4.
in onCreate of Act4, i have this code:
if (boolean) {
Intent intent = new Intent(context, Act5.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
Is it okay to call finish first before the startActivity?
Thanks in advance.
Before calling intent to start act5 from act4 call finish() method.
boolean your_variable = true;
if(your_variable)
{
startActivity(new Intent(Act4.this, Act5.class));
finish();//now this finish() method will finish Act4 so when you press back on Act5 it will return back to Act3
}
My Suggestion is to use startActivityForResult() instead of startActivity(intent) and when you finish from activity5, you can set some flag and can manage while reaching onActivityResult()
if you want to finish activity4 based on flag, do finish() in onActivityResult()
in onCreate method of your Act4 class you can put
if (yourBooleanValue) {
finish();
startActivity(yourIntent);
}
or, it would be better if you would have that boolean in your 3rd activity..
you can manage by passing some value by putExtra by activity 1/2/3 and on activity 5 you can go back to activity according to those values.

How to restart previous activity in android?

In my android app, I have an activity that displays profile information. Then it opens a new activity to make changes to the activity. After you save changes, it closes the edit activity from the edit activity, then goes back to the profile displaying activity, there I need to restart this activity to refresh the data.
Is there a way I can restart the activity that opened the current activity?
There are three ways you can achieve what you want:
Start the profile activity by calling startActivityForResult() and refresh your data in onActivityResult()
Finish() the activity when start the profile activity and then override onBackPress() in the profile activity to start the previous activity and then call finish().
Override onBackPress() in the profile activity to start the previous activity with the flag Intent.FLAG_ACTIVITY_CLEAR_TOP and refresh your data in onNewIntent()
Use Android Activity Result Pattern that communticates parent and child activities:
In your profile activity (parent):
#Override
public void onClick(View v) {
startActivityForResult(
new Intent(this, NameActivity.class),
1); // 1 to identify caller activity.
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
updateProfile();
}
}
In your editor activity (child):
#Override
public void onClick(View v) {
setResult(
RESULT_OK);
finish();
}
You won't have to "restart" Activity A.
You can call startActivityForResult(Intent, int) to start your new Activity, and then receive extras via the onActivityResult(int, int, Intent) call.
http://developer.android.com/reference/android/app/Activity.html#StartingActivities
Alternatively, look into the onResume() method, which is called every time your screen becomes visible. Assuming you persisted some state in Activity B (e.g. a SharedPreference or a database record), in onResume() you can refresh Activity A.
Activity Display;
Activity Modification;
When you startActivity M in your Activity D,don't D.finish();
then, when you M.finish(),you don't need "restart" Activity D,because it is already there.
You have two choices to "refresh the data":
1.startActivityForResult as they said.I won't explain any more.
2.make a cache.
save data in some static variables or sqlite or even sharedPreference.Then in onResume() of Activity D,get those data and show them.
Try this: If A is calling B and when B ends you need to update A, then in A launch B with startActivityForResult(). When B returns, the method onActivityResult() of A will be called. In this method (of A) you can test, if necessary, that some stored values are changed. At this point you do something like:
finish();
startActivity(intent); //start the activity A

How to maintain the previous state of an activity

I am having two activities (activity1 and activiy2) and each activity is having one button each. In activity1 i having a spinner with few options. Suppose i am selecting option 2 from this spinner and i am clicking the button in activity1, then activity2 starts. When i click back button activity1 is resumed and the same option 2 is visible (like i need). Now the problem is that if my activity2 is started and i am clicking a button in it, activity1 is started. But instead of resuming the previous state of activity1 it starts in a way that it has just created and the previous selection is changed. How can i get the same facility like back button (not the facility of going back to previous activity, i mean automatically resuming the previous state of any activity) even when i start the activity again. Simply i need to know how to maintain the previous state of an activity if it is again visited.
It is with this code I go from one activity to another when button is clicked:
Intent intent=new Intent();
intent.setClassName(getApplicationContext(),"com.myapp.activityname");
startActivity(intent);
Kindly help me.I am a beginner in android, so if any one is giving the answer please explain it a bit. Thanks in adavnce
Think I found the answer. Let me tell what I have done in simple words,
Suppose i am having two activities activity1 and activity2 and i am navigating from activity1 to activity2(i have done some works in activity2) and again back to activity 1 by clicking on a button in activity1. Now at this stage I wanted to go back to activity2 and i want to see my activity2 in the same condition when I last left activity2.
For the above scenario what i have done is that in the manifest i made some changes like this:
<activity android:name=".activity2"
android:alwaysRetainTaskState="true"
android:launchMode="singleInstance">
</activity>
And in the activity1 on the button click event i have done like this:
Intent intent=new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.setClassName(this,"com.mainscreen.activity2");
startActivity(intent);
And in activity2 on button click event i have done like this:
Intent intent=new Intent();
intent.setClassName(this,"com.mainscreen.activity1");
startActivity(intent);
Now what will happen is that whatever the changes we have made in the activity2 will not be lost, and we can view activity2 in the same state as we left previously.
I believe this is the answer and this works fine for me.
By overriding an Activity’s onSaveInstanceState event handler, you can use its Bundle parameter to save instance values. Here is an example:
#Override
public void onSaveInstanceState(Bundle outState) {
// Retrieve the View
TextView myTextView = (TextView)findViewById(R.id.myTextView);
// Save its state
outState.putString("My text",
myTextView.getText().toString());
super.onSaveInstanceState(outState);
}
The saved Bundle is passed into the onRestoreInstanceState and onCreate methods if the application is forced to restart during a session. You can then extract values from the Bundle and use them to update the Activity instance state. Here is an example:
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView myTextView = (TextView)findViewById(R.id.myTextView);
String text = “”;
if (icicle != null && icicle.containsKey("My text")) {
text = icicle.getString(TEXTVIEW_STATE_KEY);
}
myTextView.setText(text);
}
It’s important to remember that onSaveInstanceState is called only when an Activity becomes inactive, but not when it is being closed by a call to finish or by the user pressing the Back button.
Download source code or see the entire post
For activity one, you need to use the startActivityForResult() function that returns values to the parent activity which is the activity one.
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivityForResult(intent, 1);
And to receive the values from activity two without alteration of activity one, add the following code
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String text=data.getStringExtra("text");
vTxt.setText("Name 1: "+eTxt.getText()+ "\n Name 2: "+text);
}
}
}
Activity 2
add the following code to return to activity one and pass some values
Intent intent=new Intent(Activity2.this, MainActivity.class);
Bundle extra=new Bundle();
extra.putString("text", eTxt.getText().toString());
intent.putExtras(extra);
setResult(RESULT_OK, intent);
finish();
Finally you are done

Categories

Resources