IT

부모 활동에서 Fragment 메소드 호출

lottoking 2020. 7. 25. 10:19
반응형

부모 활동에서 Fragment 메소드 호출


I는 참조에 안드로이드 파편 데브 안내서 에 "활성, FragmentManager의 단편에 대한 참조를 금액하여 단편의 메소드를 호출 할 수있는 findFragmentById()findFragmentByTag()."

다음 예제는 프래그먼트 참조를 얻는 방법을 보여 주지만 프래그먼트에서 특정 메소드를 호출하는 방법은 보여주지.

누구나 접수하는 방법의 예를 들어 줄 수 있습니까? 부모 활동에서 Fragment의 특정 메소드를 호출하고 싶습니다. 감사합니다.


너무 간단해서 질문을 정확히 지 못합니다.

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.<specific_function_name>(); 

업데이트 : Kotlin을 사용하는 사람들을 위해

var fragment = supportFragmentManager.findFragmentById(R.id.frameLayoutCW) as WebViewFragment
fragment.callAboutUsActivity()

"import android.app.Fragment;"를 사용하는 경우 그런 다음 다음 중 하나를 사용하십시오.

1)

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment); 
fragment.specific_function_name(); 

여기서 R.id.example_fragment는 xml 레이아웃 내의 FrameLayout id 일 가능성이 있습니다. 또는

2)

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentByTag(“FragTagName”); 
fragment.specific_function_name(); 

여기서 FragTagName은 다음을 수행 할 때 지정된 이름입니다.

TabHost mTabHost.newTabSpec(“FragTagName”)

"import android.support.v4.app.Fragment;"를 사용하는 경우 그런 다음 다음 중 하나를 사용하십시오.

1)

ExampleFragment fragment = (ExampleFragment) getSupportFragmentManager().findFragmentById(R.id.example_fragment); 
fragment.specific_function_name(); 

또는

2)

ExampleFragment fragment = (ExampleFragment) getSupportFragmentManager().findFragmentByTag(“FragTagName”); 
fragment.specific_function_name(); 

지원 라이브러리를 사용하는 경우 다음과 같은 작업을 수행해야합니다.

FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.my_fragment);
fragment.myMethod();

  1. 지원 라이브러리 Fragment를 사용하지 않는 경우 다음을 수행하십시오.

((FragmentName) getFragmentManager().findFragmentById(R.id.fragment_id)).methodName();


2. 지원 라이브러리 조각을 사용하는 경우 다음을 수행하십시오.

((FragmentName) getSupportFragmentManager().findFragmentById(R.id.fragment_id)).methodName();


조각에서 메소드를 호출하기 전에 조각이 추가 확인하는 것이 좋습니다. null 예외를 피 청구 이와 같은 작업을 수행하십시오.

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
if(fragment.isAdded()){
  fragment.<specific_function_name>(); 
}

단편에서 활동으로 :

((YourActivityClassName)getActivity()).yourPublicMethod();

활동에서 단편으로 :

FragmentManager fm = getSupportFragmentManager ();

//if you added fragment via layout xml
YourFragmentClass fragment = 
(YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id);
fragment.yourPublicMethod();

를 통해 프래그먼트 코드를 추가하고 프래그먼트를 추가 할 때 태그 문자열을 사용한 경우 findFragmentByTag를 대신 사용하십시오.

YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag("yourTag");

FragmentManager fm = getFragmentManager(); 
MainFragment frag = (MainFragment)fm.findFragmentById(R.id.main_fragment); 
frag.<specific_function_name>(); 

내가 모르는 Java만에 C#(Xamarin.Android)이 메소드를 호출하는 데 필요한 조각 매번를 검색 할 필요가 없습니다, 아래 참조 :

public class BrandActivity : Activity
{
    MyFragment myFragment;

    protected override void OnCreate(Bundle bundle)
    {       
        // ...
        myFragment = new MyFragment();      
        // ...
    }

    void someMethod()
    {
        myFragment.MyPublicMethod();
    }
}

public class MyFragment : Android.Support.V4.App.Fragment
{
    public override void OnCreate(Bundle bundle)
    {
        // ...
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle)
    {
        // ...
    }

    public void MyPublicMethod()
    {
        // ...
    }   
}

나는 Java당신도 똑같이 할 수 있다고 생각 합니다.


또한 다음과 같은 인터페이스를 사용하여 조각 메서드를 호출합니다.

먼저 인터페이스를 만듭니다.

public interface InterfaceName {
    void methodName();
}

인터페이스를 만든 후 조각에 인터페이스를 구현합니다.

MyFragment extends Fragment implements InterfaceName {
    @overide
    void methodName() {

    }
}

활동에서 인터페이스 참조를 생성합니다.

class Activityname extends AppCompatActivity {
    Button click;
    MyFragment fragment;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);

        click = findViewById(R.id.button);

        fragment = new MyFragment();

        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               fragment.methodName();
            }
        });
    }
}

먼저 fragment같은 방법으로 방법을 만듭니다.

public void name()
{


}

당신에 activity당신이 추가

onCreate()방법 추가

myfragment fragment=new myfragment()

마지막으로 호출하려는 메소드를 호출하십시오.

fragment.method_name();

이 코드를 시도

참고 URL : https://stackoverflow.com/questions/10903077/calling-a-fragment-method-from-a-parent-activity

반응형