Removing previous activity from current activity in android - java

I have a stack of activities like A->B->C->D... Back function is enabled on all activities. Now on a specific action on activity D, I would like to move to activity B i.e. D&C gets finished now and activity B is resumed. How to achieve such transition ?

From Activity D, call Activity B with proper flags in the Intent. Intent.FLAG_ACTIVITY_CLEAR_TOP should do it.
Example code:
Intent intent = new Intent(this, B.class)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();

If you want to follow proper and ideal flow , go for "onActivityResult" method. Suppose for your example when B goes to C, at that time it will start activity C using startActivityForResult and when C goes to D then again C will use statActivityForResult to go do D.
For your case,
public class B extends Activity
{
public static int RESULT_CODE=111;
public void onCreate(Bundle SavedInstance)
{
...
....
Intent intent = new Intent(B.this,C.class);
startActivityForResult(intent,REQUEST_CODE);
}
public void onActivityResult(int requestCode,int resultCode,Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestcode == C.RESULT_OK)
{
//do you work when D is finished and comes back to B
}
}
public class C extends Activity
{
public static int RESULT_CODE=222;
public void onCreate(Bundle SavedInstance)
{
...
....
Intent intent = new Intent(C.this,D.class);
startActivityForResult(intent,REQUEST_CODE);
}
public void onActivityResult(int requestCode,int resultCode,Intent data)
super.onActivityResult(requestCode, resultCode, data);
if(requestcode == D.RESULT_OK)
{
setResult(RESULT_OK);
}
}
public class D extends Activity
{
public static int RESULT_CODE=333;
public void onCreate(Bundle SavedInstance)
{
...
....
}
//your back click function
public void onBackClick(View v)
{
setResult(RESULT_OK);
}

Just to clarify, use this API < 11:
Intent intent= new Intent(this, Login2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(Intent);
In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK
Intent intent= new Intent(this, Login2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(Intent);

Try adding this code inside some action of D Activity
C close = new C();
close.finish();
D.this.finish();
And in activity tag of manifest file add this attribute
android:launchmode="singleTop" //Or singleTask
So that you can close both c and D activity and resume into B.

Related

Refresh Activity

This is my scenario
Activity A (List of user)
Activity B (User Add Activity)
Desired Flow in app
Step 1: A ==> B
Step 2: B ==> A
Step 3 (Required flow):
Activity A has to reload itself or call loadListView() function after Activity B exits or is closed.
onCreate(){
.......
....
btnAddClient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
loadListView();
......
....
}
Try to call loadListView() in onStart or onResume methods. It will refresh the list when you come back to your activity
Getting a result from an activity
From your Activity A, call the Activity B using the startActivityForResult() method.
int RESULT_CODE = 123;
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, RESULT_CODE);
Now in your ActivityA class, write the following code for the onActivityResult() method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_CODE) {
if(resultCode == Activity.RESULT_OK){
bool status=data.getBooleanExtra("isUserAdded");
if(status)
{
loadListView();
}
}
}
} //onActivityResult
Return back to ActivityA with status.
Intent returnIntent = new Intent();
returnIntent.putExtra("isUserAdded",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();

Start activity for result not work, between different activities

I have the main page where the photo is loaded, depending on what the array on the other page contains, in order to synchronize them I used StartActivityForResult();.
It will works like this: MainActivity has a photo, i press showMore button(open showMoreActivity), at showMoreActivity change text and after i finish showMoreActivity, MainActivity load the new photo depends on new text, but real it doesn`t change photo.
Can you help me? Where the mistake is?
buttonShowMore.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ShowMore.class);
onStop();
getActivity().startActivityForResult(intent, 1);
}
});
onClick button ShowMore Listener
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {return;}
String dataStringExtra =data.getStringExtra("name1");
Picasso.get().load( dataStringExtra + ".jpg").into(imageViewFirstOfCurrentList);
}
onActivityResult method
Intent intent = new Intent();
intent.putExtra("name1", List.get(0).toString());
setResult(RESULT_OK, intent);
finish();
when close showMore Activity
try removing call to onStop() in your onClick() method

onActivityResult return data from fragment to activity

I Have an activity A that opens an activity B using startActivityForResult.
Now in activity B it's an activity fragment holder as well it contains an ActionBar with menu items.
Now whenever I press action bar button in activity B it should return data from selected fragment of an activity B not to its holder instead it should return data to activity A because it's the one who did the launch.
So it's basically passing data fragment (inside activity B) to activity B then to Activity A.
I am trying hopelessly to find a way to solve it. Is there any possible way to do it?
Disclaimer there are many ways, this is the one I prefer, not the best ever and not the perfect one, I just like this.
The easiest way, in my opinion, is to pass the data from Fragment inside B to ActivityB, then from ActivityB to ActivityA.
Step 1 to pass data from Fragment to container activity you have many ways; the one I usually use is to use an Interface:
Create interface for ActivityB
public interface IActivityB {
void setDataAAndFinish(whateverType data);
}
Implement interface in your activityB
public class InterventoActivity extends AppCompatActivity implements IInterventoActivity {
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private Bundle dataA = null;
#Override
public void setDataAAndFinish(whateverType data) {
dataA = data;
Intent intent = new Intent();
intent.putExtra("data", data)
setResult(RESULT_OK, intent);
finish();
}
}
Set activityA to request and accept return from ActivityB
first, start activityB for result and not normally
Intent i = new Intent(this, ActivityB.class);
startActivityForResult(i, 1);
Then read result
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK) {
whateverType data = data.getStringExtra("data");
}
}
}
Now from fragment
((IActivityB)getActivity()).setDataAAndFinish(myDatas);
You need to declare a function in your ActivityB like the following.
public void sendDataBackToActivityA(String dataToBePassedToActivityA) {
Intent intent = new Intent();
intent.putExtra("data", dataToBePassedToActivityA);
setResult(RESULT_OK, intent);
finish();
}
Now from the Fragment that you launched from ActivityB, just call the method on some action in your Fragment that was launched from ActivityB. So the pseudo implementation of the process in your Fragment should look like the following. Declare this function in your Fragment and invoke the function on some action in your Fragment.
public void sendDataToActivityAFromFragment(String dataToBePassed) {
((ActivityB)getActivity()).sendDataBackToActivityA(dataToBePassed);
}
This will serve your purpose I hope.
You can declare static variables in your A activity and set it from the B activity
Or you can make static class and set variables values from B Activity and get it in A activity
Or you can send the menu value from Activity B to A while you exit Activity B and start activity A by using bundle
this is how you set the data to be passed to activity A in activity B
val resultIntent = Intent()
resultIntent.putExtra(DATA, "closed")
setResult(Activity.RESULT_OK, resultIntent)
finish()
this is how you get data in activity A
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == FILE_UPLOAD_CODE) {
when (resultCode) {
Activity.RESULT_OK -> {
// data here is obtained data of the method paramater
}}}}
In your Activity A.Move from activity A to b like this:-
int YOUR_CODE=101; // it can be whatever you like
Intent i = new Intent(getActivity(), B.class);
startActivityForResult(i,YOUR_CODE);
In your Activity B in its fragment
Intent resultIntent = new Intent();
resultIntent.putExtra("NAME OF THE PARAMETER", valueOfParameter);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();
And in your Activity A's onActivityResult() function do this:-
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (resultCode==YOUR_CODE)
{
String value = (String) data.getExtras().getString("NAME OF THE PARAMETER"); //get Data here
}
}
}
In kotlin: -
In class A
startActivityForResult(Intent(context, B::class.java), 143)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == 143) {
if (data!!.extras.get("check").equals("0")) {
childpager.currentItem = 0
}
}
}
In Class B
val intent = Intent()
intent.putExtra("check", "0")
setResult(Activity.RESULT_OK, intent)

Call method when intent closes

I am making a music player application, and I am trying to implement playlists. I have a file chooser in another intent, and I would like the ListView in the mainActivity to update when the file chooser intent closes. how can I call my UpdateListView method when it closes?
start intent:
Intent intent = new Intent(this, FileChooser.class);
startActivity(intent);
Closing intent
public void closeButton(View view){
finish();
}
Any help would be appreciated! thanks!
I assume you are using your own FileChoser class, not a standard Android one:
private static final int FileChooserRequestCode = 666;
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FileChooserRequestCode);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FillChooserRequestCode) {
if (resultCode == Activity.RESULT_OK) {
// ... file is chosen
String fileName = data.getStringExtra("FileName");
} else {
... dialog is closed
}
}
}
in FileChoser you do
Intent intent = new Intent();
intent.putStringExtra("FileName", fileName);
SetResult(Activity.RESULT_OK, intent);
finish();
and
SetResult(Activity.RESULT_CANCELED);
finish();
You can use startActivityForResult() please refer the link Getting Results From Activity
static final int FILE_CHOOSER_INTENT = 1; // The request code
...
private void chooseFile() {
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FILE_CHOOSER_INTENT);
}
Call setResult pass your result data as Intent. for details refer link SetResult function
Override this in your calling activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == FILE_CHOOSER_INTENT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}

Android Getting Results from another activity

I have a problem with my code. I want to pass a String from the SecondActivity to FirstActvity. Note that the FirstActivity is not visible but its still open. when the SecondActivity is finish it passes a String to the FirstActivity.
My problem here is that when the SecondActivity ended and goes to FirstActivity, the whole application closes.
FirstActivity to SecondActivity:
Intent intent = new Intent(MainActivity.this, FileChooser.class);
startActivityForResult(intent, 0);
SecondActivity to FirstActivity:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("filePath", "/sdcard/path1");
setResult(0);
finish();
FirstActivity Result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//TODO handle here.
Intent intent = getIntent();
this.filePath = intent.getExtras().getString("filePath");
}
What is wrong with the code?
When you set the result of your SecondActivity, you only set the result code. Instead of setResult(0) use setResult(0,intent)
Also, in your FirstActivity's onActivityResult get the extra from the data argument - this.filePath = data.getExtras().getString("filePath");
Try to use
data.getExtras().getString("filePath");
instead of
intent.getExtras().getString("filePath");`
Try with Bundle:
First Activity;
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class);
Bundle bundle = new Bundle();
bundle.putString("filePath","/sdcard/path1");
intent.putExtras(bundle);
startActivity(intent);
}
Second Activity:
public void activity_value() {
Intent i = getIntent();
Bundle extras=i.getExtras();
if(extras !=null) {
value = extras.getString("filePath");
}
}
try this
example.It solves your problem.

Categories

Resources