1

Navigation Drawer에서 작업하고 있으며 누군가가 슬라이딩 메뉴에있는 항목을 클릭하면 testActivity.java 클래스를 호출하려고합니다. 현재 MainActivity.java calss가 FragmentOne을 호출하고 있습니다. FragmentTwo이 FragmentThree 슬라이딩 메뉴 항목 selected..I이 testActivity.java프래그먼트에서 Activity 클래스를 호출하는 방법

와 이것을 대체 할 여기에 내 MainActivity.java

public class MainActivity extends FragmentActivity { 
final String[] data ={"one","two","three"}; 
final String[] fragments ={ 
     "core.layout.FragmentOne", 
     "core.layout.FragmentTwo", 
     "core.layout.FragmentThree"}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String> (getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data); 

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout); 
    final ListView navList = (ListView) findViewById(R.id.drawer); 
    navList.setAdapter(adapter); 
    navList.setOnItemClickListener(new OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){ 
        drawer.setDrawerListener(new DrawerLayout.SimpleDrawerListener(){ 
          @Override 
          public void onDrawerClosed(View drawerView){ 
            super.onDrawerClosed(drawerView); 
            FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
            tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos])); 
            tx.commit(); 
          } 
        }); 
        drawer.closeDrawer(navList); 
      } 
    }); 
    FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); 
    tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0])); 
    tx.commit(); 
} 

}

여기 내 FragmentOne.java의 CALSS이입니다 .. 다른 모든 FragmentTwo 및 FragmentThr ee.java 클래스는 이와 같은 코드를가집니다.

 public class FragmentOne extends Fragment { 


public static Fragment newInstance(Context context) { 
    FragmentOne f = new FragmentOne(); 

    return f; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null); 

    View view = inflater.inflate(R.layout.fragment_one,container, false); 

    return root; 


} 

}

이 내 testActivity.java 클래스

public class testActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.test); 


} 

} 조각에서

답변

3

당신이 getActivity를 사용하여 부모 FragmentActivity 인스턴스에 액세스 할 수있다(). 그리고 그 인스턴스가 하나의 활동이기 때문에이를 사용하면 다른 활동을 간단하게 호출 할 수 있습니다.

Intent myIntent = new Intent((ParentActivity)getActivity(), YourOtherActivity.class); 
(ParentActivity)getActivity().startActivity(myIntent); 
+0

FragmentOne 클래스에서 이와 같은 의미입니까? .. 작동하지 않습니까? 의도 의도 = 새로운 의도 (getActivity(), testActivity.class); getActivity(). startActivity (intent); – san88

+0

나는 이것들을 위에 올려 놓았습니다. 오류나 런타임 오류는 없습니다. 그러나 레이아웃 파일을 단순히 표시하지 마십시오. 공용 뷰 onCreateView (LayoutInflater inflater, ViewGroup 컨테이너, Bundle savedInstanceState) { ViewGroup root = (ViewGroup) inflater.inflate (R.layout.fragment_one, null); 뷰보기 = inflater.inflate (R.layout.fragment_one, container, false); 인 텐트 intent = 새 인 텐트 (getActivity(), testActivity.class); getActivity(). startActivity (intent); return root; } – san88

+0

getActivity를 ParentActivity로 캐스팅 해보세요. 내 대답을 편집하여 샘플 코드를 추가했습니다. – Sushil