0

I want getting a result from an activity, I try to follow the example code, but startActivityForResult() has been deprecated,

I tried this , but I don't know how to change requestCode

    ActivityResultLauncher<Intent> activityResultLaunch = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback<ActivityResult>() {
                @Override
                public void onActivityResult(ActivityResult result) {
                    if (result.getResultCode() == RESULT_OK) {
                        aMemo[requestCode] = it.getStringExtra("memo"); 
                        aa.notifyDataSetChanged(); 
                    }
                }
            });

this is my all old code (inculding deprecated method)

public class MainActivity extends AppCompatActivity
        implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {

    String[] aMemo = { 
            "1. Touch to Edit MEMO",
            "2.","3.","4.","5.","6." };
    ListView lv; 
    ArrayAdapter<String> aa; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView)findViewById(R.id.listView);
        aa = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, aMemo);

        lv.setAdapter(aa);    
        lv.setOnItemClickListener(this);
        lv.setOnItemLongClickListener(this);
    }

    public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
        Intent it = new Intent(this, Edit.class);
        it.putExtra("number", pos+1);      
        it.putExtra("memo", aMemo[pos]); 
        startActivityForResult(it, pos); 
    }

    public boolean onItemLongClick(AdapterView<?> a, View v, int pos, long id) {
        aMemo[pos] = (pos+1) + "."; 
        aa.notifyDataSetChanged();  
        return true;                
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent it) {
        if(resultCode == RESULT_OK) {
            aMemo[requestCode] = it.getStringExtra("memo"); 
            aa.notifyDataSetChanged(); 
        }
    }
}
Xiayan
  • 5
  • 3
  • There are two ways with ActivityResultContracts.StartActivityForResult. This answer can be useful for you. https://stackoverflow.com/a/67887946/20839582 – sajidjuneja Jan 04 '23 at 07:03

1 Answers1

0

Java

public void openSomeActivityForResult() {
    Intent intent = new Intent(this, SomeActivity.class);
    someActivityResultLauncher.launch(intent);
}

// You can do the assignment inside onAttach or onCreate, i.e, before the activity is displayed
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    // There are no request codes
                    Intent data = result.getData();
                    doSomeOperations();
                }
            }
        });

Kotlin

fun openSomeActivityForResult() {
    val intent = Intent(this, SomeActivity::class.java)
    resultLauncher.launch(intent)
}

var resultLauncher = registerForActivityResult(StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data
        doSomeOperations()
    }
}
Kamran Khan
  • 408
  • 2
  • 10