how to send value through intent after finishing an activity

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”.

  • What files should I ignore in an Android Project on SVN
  • Offset shape within a ShapeDrawable
  • Toolbar Navigation Hamburger Icon missing
  • Disable pan/zoom in com.google.android.maps.MapView
  • Why won't Eclipse/adb detect my Samsung Galaxy Tab device on my Mac?
  • Horizontally center Views inside Android GridLayout
  • 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!

    Related posts:

    How to check for unrestricted Internet access? (captive portal detection)
    What Do We Use for Android N Network Security Configuration for a Self-Signed Certificate?
    Google Analytics event tracking - max string length
    android: Animate color change from color to color
    Android ListView Pull Down to Refresh
    Is MD5 guaranteed to be available for use with MessageDigest in Android?
  • Is it possible to import messages into the Android emulator?
  • Android x86 emulator extremly slow
  • Android Camera Server Died and Camera Error - 100
  • Execution failed for task ':app:compileDebugKotlin'
  • How to achieve the following animation in android material design?
  • Espresso freezing on view with looping animation
  • 8 Solutions collect form web for “how to send value through intent after finishing an activity”

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

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

    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)
    

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

    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
    
        }
    

    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.”

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

    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.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.