从父活动中调用片段的方法 [英] Call a fragment's method from parent activity
问题描述
大家好,
[简介] 我创建了一个 ScreenSlide Pager (扩展FragmentActivity)可处理MainActivity中的4个片段.我也有一项服务,用于处理蓝牙通信套接字(带有RPI). 该服务可以通知MainActivity,该服务通过Handler接收.
[Introduction] I have created a ScreenSlide Pager (extending FragmentActivity) to handle 4 fragments in MainActivity. I also have a service, handling a bluetooth communication socket (with a RPI). This service can notify MainActivity which receives it through a Handler.
在这里,放置在MainActivity中:
Here it is, placed in MainActivity:
class IncomingHandler extends Handler {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case BtCommunication.MSG_SET_VALUE:
Log.d("SERVICE", "Received from service: " + msg.arg1);
break;
case BtCommunication.MSG_GET_SOCKET_STATE:
if(msg.arg1 == 1){
Log.d("SERVICE", "Socket state: UP");
onSocketUp();
} else {
Log.d("SERVICE", "Socket state: DOWN");}
break;
default:
super.handleMessage(msg);
}
}
}
我有一个片段(MainFragment),该片段首先在OnCreate()中的任何其他片段之前被调用:
I have a fragment (MainFragment) that is called first before any other fragment in the OnCreate():
if(savedInstanceState == null){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.pager, new MainFragment());
ft.commit();
}
然后,在MainActivity的ScreenSlidePagerAdapter中以这种方式处理片段.
Then, the fragments are handled this way in the ScreenSlidePagerAdapter of the MainActivity.
@Override
public Fragment getItem(int position) {
Log.d("mPager.getCurrentItem", "" + mPager.getCurrentItem());
if(position == 0){
actionBar.setTitle("Accueil - Connexion");
Log.d("Fragment", "MainFragment");
return new MainFragment();
}else if(position == 1){
actionBar.setTitle("Second fragment");
Log.d("Fragment", "SecondFragment");
return new SecondFragment();
}else if(position == 2){
Log.d("Fragment", "ThirdFragment");
return new ThirdFragment();
}else if(position == 3){
Log.d("Fragment", "ItemFragment");
return new ItemFragment();
}else{
Log.d("Fragment", "ElseFragment (MainFragment)");
return new MainFragment();
}
}
虽然套接字没有打开,但我希望他显示一个正在加载的动画(动画效果很好):
While the socket isn't up, I want him to display a loading animation (animation is working well) :
一旦连接套接字打开,我在此片段中就有一个方法来设置不可见/消失的加载动画. [/简介]
Once the connection socket is UP,I have a method in this fragment to set invisible/gone the loading animation. [/Introduction]
public void onSocketUp(){
ArcLoader.setVisibility(View.GONE);
Log.d("MAIN FRAGMENT","Arc Loader GONE");
}
我想你猜对了:每当套接字状态从DOWN切换到UP时,我都想从活动中调用该方法.
I assume you guessed it: I want to call that method from the activity whenever the socket state switches from DOWN to UP.
以下是我与之争吵的活动方法.一旦处理程序从服务检测到通知,就可以正确地调用她,但是我无法调用该片段的方法,因为findfragmentbyId返回mFrag作为空片段(?!).
The following is the activity's method I'm having a brawl with. She's correctly called once the Handler detects a notification from the service, but i can't call the fragment's method because the findfragmentbyId returns mFrag as a null fragment (?!).
不应该这样,因为它是第一个被调用的片段,而且我也不会破坏它停留在上面).
It shouldn't since it's the first fragment called and I don't destroy it staying on it).
此外,使用事务创建新片段失败(onSaveInstanceState之后无法执行此操作).
Besides, the creation of a new fragment with the transaction fails (Can not perform this action after onSaveInstanceState).
private void onSocketUp()
{
//Fragment mainFrag = getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":" + mPager.getCurrentItem());
Fragment mainFrag = getSupportFragmentManager().findFragmentById(R.id.mainfragment);
Log.d("MAIN ACTIVITY", "onSocketUp()");
if (mPager.getCurrentItem() == 0 && mainFrag != null) {
mainFrag.onSocketUp();
Log.d("SERVICE", "onSocketUp() called - Main Fragment instantiated");
}
else {
MainFragment newFragment = new MainFragment();
Bundle args = new Bundle();
args.putString(MainFragment.ARG_SOCKET_UP, "UP");
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.pager, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
我希望我能给您所有足够的信息,以获取问题的根源,如果您希望我提供任何其他代码,请不要犹豫.
I hope i gave you all the enough information to get where does my problem come from, don't hesitate if you want me to supply any additional code.
我本来可以阅读的所有其他主题直到现在都没有帮助.
All the other topics i could have read haven't helped until now.
提前谢谢您.
推荐答案
如果要从活动中调用片段方法,请执行以下操作:
If you want to call a fragment method from activity just do the following :
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.mainfragment);
if (fragment instanceof MainFragment){
((MainFragment)fragment).onSocketUp();
}
这篇关于从父活动中调用片段的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!