Search code examples
androidandroid-fragmentsandroid-intentandroid-activity

How to intent from activity to specific fragment


I have a home menu with a bottom navbar menu, in the navbar menu the last tab, I create a button to intent to other activity,

this button from An Activity with BottomNavbar open another activity

  @OnClick(R.id.scanQR)
    public void onScan() {
        startActivity(new Intent(getContext(),Action_camera.class));
    }

in other activity I just use it to get QRcode and send QRcode.result (String) back to the Last Fragment I used in this case Fragment No 3

if I use intent to parent activity from fragment no 3 it will just show fragment no one

@OnClick(R.id.cariqr)
    public void onClickqr() {
        Bundle args= new Bundle();
        args.putString("kodebmnscan",textView.getText().toString().trim());
        frag_asset_admin bundle = new frag_asset_admin();
        bundle.setArguments(args);
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.container, bundle).commit();
    }

how to back to navbar menu the last tab from activity


Solution

  • I did not understand your question properly but as much I understand I can help you understand how to move through fragments and activities:

    To move from activity to the activity or fragment to activity you use simple intent:

    Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
    startActivity(intent);
    

    to load fragment in a ViePager u simple use FragmentManager like:

    getSupportFragmentManager().beginTransaction().replace(R.id.ViewPagerID, context).commit();
    

    So, to start an activity to get results from, you need to get results of QR code back to the home activity. You will do steps like these:

    to start an activity for results:

    startActivityForResult(new Intent(context,QRCODE.class),REQUEST_CODE);
    

    to send back data

    Intent returnIntent = new Intent();
    returnIntent.putExtra("result",result);
    setResult(Activity.RESULT_OK,returnIntent);
    finish();
    

    If you don't want to return data:

    Intent returnIntent = new Intent();
    setResult(Activity.RESULT_CANCELED, returnIntent);
    finish();
    

    to receive data back in 'homeactivity':

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
    super.onActivityResult(requestCode, resultCode, data);
    
    if (requestCode == LAUNCH_SECOND_ACTIVITY) {
        if(resultCode == Activity.RESULT_OK){
            String result=data.getStringExtra("result");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
      } 
    }
    

    If I did not answer your question please elaborate on your question and I will try to answer.