如何在片段中使用 onNewIntent(Intent intent) 方法?
How to use method onNewIntent(Intent intent) inside a Fragment?
我正在尝试通过我的设备使用 NFC 硬件。但是,问题是当我注册 Activity 以接收 Intent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
我在 Activity 而不是片段中收到结果。有没有办法在 Fragment 中处理这个结果?
提前致谢!
onNewIntent
属于 Activity,因此您不能将其包含在您的片段中。你可以做的是在数据到达 onNewIntent
时将数据传递给你的片段,前提是你有片段的引用。
Fragment fragment;
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Check if the fragment is an instance of the right fragment
if (fragment instanceof MyNFCFragment) {
MyNFCFragment my = (MyNFCFragment) fragment;
// Pass intent or its data to the fragment's method
my.processNFC(intent.getStringExtra());
}
}
我通过以下方式解决了我的问题:
在MyActivity.java
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
在MyFragment.java
@Override
public void onStart() {
super.onStart();
if (getActivity() != null && getActivity().getIntent().hasExtra(...)) {
// do whatever needed
}
}
我们以更动态的方式处理调用以支持任何片段,方法是在 Activity
中添加如下内容:
// ...
public class ActivityMain extends AppCompatActivity {
// ...
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Fragment fragment = getFragment();
if (fragment != null) {
try {
Method method = fragment.getClass().getMethod("onNewIntent", Intent.class);
method.invoke(fragment, intent);
} catch (NoSuchMethodException ignored) {
} catch (IllegalAccessException | InvocationTargetException e) {
Log.e(TAG, "Failed to call onNewIntent method for class: " + fragment.getClass().getSimpleName());
}
}
}
public Fragment getFragment() {
// For NAVIGATION-DRAWER only
// (replace below logic, if you use View-Pager).
FragmentManager manager = this.getSupportFragmentManager();
manager.executePendingTransactions();
return manager.findFragmentById(R.id.my_main_content);
}
}
然后在每个根中 Fragment
简单地听:
@SuppressWarnings("unused")
public void onNewIntent(Intent intent) {
Log.i("MyTag", "onNewIntent: " + intent);
}
使用LiveData
:
存储库:
class IntentRepo {
private val _intent = MutableLiveData<Intent>()
val get: LiveData<Intent> = Transformations.map(_intent) { it!! }
fun set(intent: Intent) { _intent.value = intent }
}
Activity 视图模型:
class MainViewModel(intentRepo: IntentRepo) : ViewModel() {
val intent = intentRepo
}
Activity
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
viewModel.intent.set(intent)
}
片段
viewModel.intent.get.observe(viewLifecycleOwner, {
// Your intent: $it
})
我正在尝试通过我的设备使用 NFC 硬件。但是,问题是当我注册 Activity 以接收 Intent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
我在 Activity 而不是片段中收到结果。有没有办法在 Fragment 中处理这个结果?
提前致谢!
onNewIntent
属于 Activity,因此您不能将其包含在您的片段中。你可以做的是在数据到达 onNewIntent
时将数据传递给你的片段,前提是你有片段的引用。
Fragment fragment;
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Check if the fragment is an instance of the right fragment
if (fragment instanceof MyNFCFragment) {
MyNFCFragment my = (MyNFCFragment) fragment;
// Pass intent or its data to the fragment's method
my.processNFC(intent.getStringExtra());
}
}
我通过以下方式解决了我的问题:
在MyActivity.java
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
在MyFragment.java
@Override
public void onStart() {
super.onStart();
if (getActivity() != null && getActivity().getIntent().hasExtra(...)) {
// do whatever needed
}
}
我们以更动态的方式处理调用以支持任何片段,方法是在 Activity
中添加如下内容:
// ...
public class ActivityMain extends AppCompatActivity {
// ...
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Fragment fragment = getFragment();
if (fragment != null) {
try {
Method method = fragment.getClass().getMethod("onNewIntent", Intent.class);
method.invoke(fragment, intent);
} catch (NoSuchMethodException ignored) {
} catch (IllegalAccessException | InvocationTargetException e) {
Log.e(TAG, "Failed to call onNewIntent method for class: " + fragment.getClass().getSimpleName());
}
}
}
public Fragment getFragment() {
// For NAVIGATION-DRAWER only
// (replace below logic, if you use View-Pager).
FragmentManager manager = this.getSupportFragmentManager();
manager.executePendingTransactions();
return manager.findFragmentById(R.id.my_main_content);
}
}
然后在每个根中 Fragment
简单地听:
@SuppressWarnings("unused")
public void onNewIntent(Intent intent) {
Log.i("MyTag", "onNewIntent: " + intent);
}
使用LiveData
:
存储库:
class IntentRepo {
private val _intent = MutableLiveData<Intent>()
val get: LiveData<Intent> = Transformations.map(_intent) { it!! }
fun set(intent: Intent) { _intent.value = intent }
}
Activity 视图模型:
class MainViewModel(intentRepo: IntentRepo) : ViewModel() {
val intent = intentRepo
}
Activity
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
viewModel.intent.set(intent)
}
片段
viewModel.intent.get.observe(viewLifecycleOwner, {
// Your intent: $it
})