Reputation: 153
I have an activity that shows a gridview of items. when I click in any of them, I move to another activity showing the information of the item. I also have the option to go back (gridview activity) or delete it.
If I delete it, I need to refresh the gridview, so I need to send a variable saying "eey, I have deleted an item. you need to refresh".
I guess I need to open the second activity with startActivityForResult, but I don't know how should I set the value.
option 1 (back arrow): finishes the activity. no result back. option 2 (delete item): finishes the activity and sets a result back.
Thanks in advance!
Upvotes: 10
Views: 14627
Reputation: 9827
You can set your custom result. Like DELETE_ITEM
.
On your delete button do something like this :
public static int DELETE_ITEM = 1234;
Intent intent = new Intent();
intent.putExtra("ITEM_ID", YOUR ITEM ID);
setResult(DELETE_ITEM , intent);
Now on your activity result do something like this :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == DELETE_ITEM) {
// CODE TO DELETE ITEM
super.onActivityResult(requestCode, resultCode, intent);
}
}
Upvotes: 15
Reputation: 52790
Well, I have done this way:
Start ActivityB from AcivityA by startActivityForResult:
Intent intent = new Intent(AcivityA.this, ActivityB.class);
startActivityForResult(intent, 19);
In ActivityB use setResult
for sending data to AcivityA:
@Override
public void finish() {
Intent intent = new Intent();
intent.putExtra("flag",true);
setResult(RESULT_OK, intent);
super.finish();
}
In ActivityA receive data which were sent by ActivityB as onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 19) {
if(resultCode == RESULT_OK) {
boolean flagResult = data.getBooleanExtra("flag");
Log.i("Result", ""+flagResult);
}
}
}
Hope this will help you.
Upvotes: 1
Reputation: 1634
Hope this helps
First class
Intent i = new Intent(this, Second.class);
startActivityForResult(i, 1);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
//your code
}
if (resultCode == RESULT_CANCELED) {
// Write your code if there's no result
}
}
}
Second class
Intent returnIntent = new Intent();
returnIntent.putExtra("result", "your message");
setResult(RESULT_OK, returnIntent);
finish();
Upvotes: 13
Reputation: 2235
You just need after you delete the image call setResult with appropriate result code and maybe data.
I think most correct way to achieve this is to return from your Image viewing activity custom user result. It can be achieved if you write something like this in your image viewer activity.
public static final int RESULT_IMAGE_DELETED = RESULT_FIRST_USER;
public static final int RESULT_EXTRA_IMAGE_ID = "extra.imageid";
void onImageDeleted(int imageId) {
Bundle resultData = new Bundle();
resultData.putInt(RESULT_EXTRA_IMAGE_ID, imageId);
setResult(RESULT_IMAGE_DELETED);
finish();
}
You might be need to set default result in your onCreate method
@Override
public void onCreate(Bundle savedInstanceState) {
// init stuff
setResult(RESULT_OK);
}
On your grid activity you must to check result code of your image viewer activity, and if it is not ok — make check your grid to it's data changed.
public static final int REQUEST_VIEW_IMAGE = 1;
void startImageViewing(int imageId) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(ImageViewActivity.EXTRA_IMAGE_ID);
startActivityForResult(intent, REQUEST_VIEW_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Bundle data) {
if (requestCode == REQUEST_VIEW_IMAGE) {
if (resultCode != RESULT_OK) {
updateImageGridView();
}
}
}
Upvotes: 0
Reputation: 4348
No need to finish activity
//reload the adapter and use grid.invalidateViews(); Method
adapter = new AppAdapter(MyApp.this, MyApp.this, data, show_collection );
grid.invalidateViews();
grid.setAdapter(adapter);
GridView has an invalidateViews() method.
when you call this method: "all the views to be rebuilt and redrawn."
Upvotes: 0
Reputation: 20355
Say I wish to go from activity A to activity B, and pass a value to B, and finally finish A.
Follow this:
Somewhere in A:
Intent goToActivityBIntent = new Intent(A.this, B.class);
goToActivityBIntent.putExtra("com.example.myapp.data", data);
startActivity(goToActivityBIntent);
finish(); // close A
Receive the data in B:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
// receive the data
Bundle extras = getIntent().getExtras();
if (extras != null) {
data = extras.getString("com.example.myapp.data");
}
// Some stuff
}
Upvotes: 0
Reputation: 1945
we can start the new activity.Send value through intent using this..
Intent main= new Intent(MainActivity.this, SecondActivity.class); main.putExtra("JobId", jobid); startActivityForResult(audio, requestCode);
Receive the value in SecondActivity like this..
int jobid = getIntent().getExtras().getInt("jobId");
Upvotes: 0
Reputation: 157437
yes you have to use startActivityForResult
. Before calling finish on the Activity that shows information of the item, you have to call
setResult(resultCode, data);
finish();
in the GridView's Activity will be invoked
protected void onActivityResult(int arg0, int resultCode, Intent data)
Upvotes: 5