简体   繁体   中英

How to use Activities within fragments in Navigation Drawer

I am having an issue with using activities in fragments.

I have two activities , activity one will send data to two using intents.

Activity one is basically nfc scan part and tag information is sent using intents, Activity two will receive and display the info.

I am trying to put activities in fragments and use with navigation drawer UI.

How do i achieve this?

Activity 1 Code :

 public class Activity2 extends Activity {

private NfcAdapter mAdapter;
private PendingIntent mPendingIntent;
private IntentFilter[] mFilters;
private String[][] mTechLists;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mAdapter = NfcAdapter.getDefaultAdapter(this);        
    mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);        
    mFilters = null;
    mTechLists = null;  

    Intent intent = getIntent();       
    String action = intent.getAction();
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) 
    {             
       onNewIntent(intent);
    }

}

@Override
protected void onResume() {
    super.onResume();

    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);

}

@Override
public void onPause() {
    super.onPause();

    mAdapter.disableForegroundDispatch(this);
}


@Override
public void onNewIntent(Intent intent) {
    Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);

    Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);                

    startnfcread(tag);
}

private void startnfcread(Tag tag){
    try {

        NfcV nfcv = NfcV.get(tag);
        if(nfcv != null){

            nfcv.connect();
            Intent newActivity = new Intent(this, Activity2.class);
            newActivity.putExtra("TagID", tag.getId()); 

    startActivity(newActivity);

            nfcv.close();
        }

    } catch (Exception e) {
        Log.e("NFC error", e.toString());
        Toast.makeText(this, "NFC failed", Toast.LENGTH_SHORT).show();
    }    
    }

Activity 2:

public class Activity2 extends Activity {

 private String displayID = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

    setContentView(R.layout.results);

    TextView resultIdView = (TextView) findViewById(R.id.Tag_id);

    Bundle extras = getIntent().getExtras();        
    if(extras !=null)
    {
        byte[] TagID = extras.getByteArray("TagID");
        displayID = toHex(TagID);

        resultIdView.setText(displayID);                
    }
}   
}

I have taken the navigation drawer example from http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/ .

There is a main drawer activity and different fragments, how can i use the activity with these fragments. When i scan nfc tag from activity 1 it will send tag id to activity 2 and display tag id.

Same concept how do use with fragments, like from fragment1 scan tag happens and displays the tag id in fragment 2.

kumar

You should read some basic docs and guides about how to deal with Fragments. Fragments ahve pretty similar lifecycle callback to Activities, so you should be able to convert your code quickly - according to its complexity.

Briefly. Create an Activity which will server as a container for you fragments (just basic Activity with some simple layout). Then in this Activity use FragmentManager to add your Fragments to it. You can be adding or removing Fragments from you Activities on the fly, or just showing/hiding them, that is completely up to you. Google has some nice examples and guides which should server you well to start. Communication among Fragments could be once again done with Intents (see getArguments() ), custom interfaces, you can use an event Bus (see Otto),...I would start with Intents..

You can pass a Bundle to a fragment.

Bundle b = new Bundle();
b.putExtra("TagID", tag.getId());
FragmentVedioView fv = new FragmentVedioView();
fv.setArguments(b);
SFM.beginTransaction().replace(id, fv, "FragmentVedioView").commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM