在处理片段onNewIntent [英] Handling onNewIntent in Fragment

查看:223
本文介绍了在处理片段onNewIntent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个使用NFC读取存储在它的一些数据的应用程序。我的应用程序使用的片段和片段不来onNewIntent()方法。一直以来,我读的数据与我不同的类,它处理NFC相关的操作完成后,我需要做的唯一的事情就是更新片段内的TextView。然而,该实现也可以用来新意图传递给片段。

I am writing an application that uses NFC to read some data stored on it. My application uses Fragments and Fragment don't come with onNewIntent() method. Since, the data I am reading is done with my separate class which handles NFC related operation, the only thing I need to do is update the TextView inside the Fragment. However this implementation can also be used to pass new Intent to the Fragment.

这是我目前的执行,这使得使用接口。在收到后的新意图和NFC相关检查成功,我呼吁听众。这是它承载片段FragmentActivity。

Here is my current implementation which makes use of an interface. I am calling the listener after new Intent is received and NFC related checks succeeds. This is the FragmentActivity which hosts Fragment.

public class Main extends FragmentActivity implements
    ActionBar.OnNavigationListener {

private Bundle myBalanceBundle;
private NFC nfcObj;
private NewBalanceListener newBlanceListener;

@Override
public void onNewIntent(Intent intent) {
    setIntent(intent);
}

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

private void getNFCState() {
    //Other NFC related codes
    else if (nfc_state == NFC.NFC_STATE_ENABLED){
        readNFCTag();
    }
}

private void readNFCTag() {
    //Other NFC related codes
    if (getIntent().getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
        nfcObj.setTag((Tag) getIntent().getParcelableExtra(
                NfcAdapter.EXTRA_TAG));
        nfcObj.readQuickBalance();

        transitQuickReadFragment(nfcObj.getCurrentBalance());
    }
}

private void transitQuickReadFragment(String balance) {
    // Creates a balance bundle and calls to select MyBalance Fragment if it
    // is not visible. Calls listener is it is already visible.
    if (actionBar.getSelectedNavigationIndex() != 1) {
        if (myBalanceBundle == null)
            myBalanceBundle = new Bundle();

        myBalanceBundle.putString(Keys.BALANCE.toString(), balance);

        actionBar.setSelectedNavigationItem(1);
    } else {
        newBlanceListener.onNewBalanceRead(balance);
    }
}

@Override
public boolean onNavigationItemSelected(int position, long id) {
    // Other fragment related codes
    fragment = new MyBalance();
    fragment.setArguments(myBalanceBundle);
    newBlanceListener = (NewBalanceListener) fragment;
    // Other fragment related codes
}

// Interface callbacks. You can pass new Intent here if your application
// requires it.
public interface NewBalanceListener {
    public void onNewBalanceRead(String newBalance);

}
}

这是MyBalance片段有TextView的需要,每当NFC读取进行更新:

This is MyBalance Fragment which has TextView that needs to be updated whenever NFC is read:

public class MyBalance extends Fragment implements NewBalanceListener {

private TextView mybalance_value;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    //Other onCreateView related code

    Bundle bundle = this.getArguments();
    if (bundle != null)
        mybalance_value.setText(bundle.getString(Keys.BALANCE.toString(),
                "0.00"));
    else
        mybalance_value.setText("0.00");

    //Other onCreateView related code
}

@Override
public void onNewBalanceRead(String newBalance) {
    mybalance_value.setText(newBalance);
}
}

这code完美的作品像预期为我的应用程序,但是,我想知道是否有更好的方式来处理来自片段新意图是什么?

This code works perfectly like expected for my application but, I want to know if there is better way to handle new Intent from Fragments?

推荐答案

没有,有没有更好的办法。片段可以活得比较长的活动,并不一定依赖于它们在所有所以提供了新的意图就没有意义了。

No, there is no better way. Fragments can live longer than Activities and are not necessarily tied to them at all so providing new intents would not make sense.

顺便说一句,你有你的code几个错误:)

Btw, you have a few bugs in your code :)

if (actionBar.getSelectedNavigationIndex() != 1) {

魔术数字是坏!使用常量。

Magic numbers are bad! use a constant.

    if (myBalanceBundle == null)
        myBalanceBundle = new Bundle();

    myBalanceBundle.putString(Keys.BALANCE.toString(), balance);
    actionBar.setSelectedNavigationItem(1);

我们已经知道,navigationitem设定为1

we already know that the navigationitem is set to 1

} else {
    newBlanceListener.onNewBalanceRead(balance);

添加一个空检查。用户可能从来没有选择的导航项目。

Add a null check. The user might have never selected a navigation item.

这篇关于在处理片段onNewIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆