2011-08-19 4 views
0

두 개의 탭이 있고 각각의 탭에 별도의 활동이 표시되는 TabActivity를 만들었습니다. 사용자가 탭을 변경하면 이러한 두 가지 작업에 모두 정의 된 onConfigurationChanged 메서드를 호출 할 수 없습니다. 여기에 코드입니다 :TabActivity에 추가 된 개별 활동의 메서드를 호출하는 방법?

public class MyTabActivity extends TabActivity implements OnTabChangeListener { 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tab_page); 

    Resources res = getResources(); 
    TabHost tabHost = getTabHost(); 
    TabHost.TabSpec spec = null; 
    Intent intent = null; 

    tabHost.setOnTabChangedListener(this); 


// ACTIVITY 1 
    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, Activity1.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("act1").setIndicator("ACT1", 
         res.getDrawable(R.drawable.tab_home)) 
        .setContent(intent); 
    tabHost.addTab(spec); 

// ACTIVITY 2 
    // Create an Intent to launch an Activity for the tab (to be reused) 
    intent = new Intent().setClass(this, Activity2.class); 

    // Initialize a TabSpec for each tab and add it to the TabHost 
    spec = tabHost.newTabSpec("act2").setIndicator("ACT2", 
         res.getDrawable(R.drawable.tab_home)) 
        .setContent(intent); 
    tabHost.addTab(spec); 
} 


    @Override 
    public void onTabChanged(String arg0) { 
     Log.d("DGTabActivity", "tab changed"); 

     if (tabHost.getCurrentTab() == 0) { 
      // How to call method on Act1?? 
         callOnConfigurationChanged("com.tab.Activity1"); // doesn't work 
     } 
     else if (tabHost.getCurrentTab() == 1) { 
      // How to call method on Act2?? 
     } 
    } 

나는 또한 방법은 onTabChanged 방법 내에서의 onConfigurationChanged 호출하는 다음 코드를 사용하려고하지만, 예외가 발생합니다 : 당신의 시간을

private void callOnConfigurationChanged (String className) { 
    try{ 
     //onConfigurationChanged 
     //Class amnClass = Class.forName("android.app.ActivityManagerNative");  

     Class amnClass = Class.forName(className); 
     Object amn = null; 
     Configuration config = null; 
     Resources res = null; 


     // res = amn.getResources(); 
     Method methodGetConfiguration = amnClass.getMethod("getResources"); 
     methodGetConfiguration.setAccessible(true); 
     res = (Resources) methodGetConfiguration.invoke(amn); 

     config = res.getConfiguration(); 

     // amn.onConfigurationChanged(config); 
     Method methodOnConfigurationChanged = amnClass.getMethod("onConfigurationChanged"); 
     methodOnConfigurationChanged.setAccessible(true); 
     methodOnConfigurationChanged.invoke(amn, config); 

    } 
    catch (Exception e) { 
     Log.d("Tab host Exception" , e.toString()); 
    } 

} 

감사합니다.

환호 애니는

+0

예외는 무엇입니까? –

답변

0

당신은 텐트 필터를 사용할 수 있습니다. 브로드 캐스트 구성 - 탭 활동에서 이벤트를 변경하고 탭의 활동에 리스너를 추가하십시오.

+1

감사. 나는 그것을 알아. 탭이 변경되면 해당 활동의 onResume 메소드가 호출됩니다. onResume 메서드에서 데이터를 새로 고쳤습니다. 다시 한번 감사드립니다. – user898195

+0

다행 당신을 도울 수있어! – superM

관련 문제