繁体   English   中英

如何在导航抽屉中的片段中使用活动

[英]How to use Activities within fragments in Navigation Drawer

我在片段中使用活动时遇到问题。

我有两个活动,活动之一将使用意图将数据发送到两个。

活动1基本上是nfc扫描部分,并且使用意图发送标签信息,活动2将接收并显示该信息。

我试图将活动分成多个片段,并与导航抽屉UI一起使用。

我该如何实现?

活动1代码:

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

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

我已从http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/中获取了导航抽屉示例。

有一个主要的抽屉活动和不同的片段,我如何将这些片段用于活动。 当我从活动1扫描nfc标签时,它将标签ID发送到活动2并显示标签ID。

具有相同概念的片段如何使用,例如来自fragment1扫描标签,并在片段2中显示标签id。

库马尔

您应该阅读一些有关如何处理片段的基本文档指南 片段的生命周期回调与活动非常相似,因此您应该能够根据其复杂性快速转换代码。

简要地。 创建一个将作为片段的容器的服务器的活动(只是具有一些简单布局的基本活动)。 然后在该活动中使用FragmentManager将片段添加到其中。 您可以动态添加或删除片段,也可以仅显示/隐藏片段,这完全取决于您。 Google提供了一些不错的示例和指南,可以帮助您入门。 片段之间的通信可以再次使用Intents(请参见getArguments() ),自定义接口来完成,您可以使用事件总线(请参见Otto),...我将从Intents开始。

您可以将Bundle传递给片段。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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