2016-05-19 8 views
0

私はアプリケーションを作成しようとしています。フラグメントで戻るボタンをクリック使用するとき、私は、フラグメントからの私のメインのレイアウトに戻りたい、次のコード -バック・ボタンをクリックするとフラグメントからメイン・レイアウトに戻る方法

public class MainActivity extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener,LocationListener{ 
DrawerLayout mDrawerLayout; 
ListView mDrawerList; 
@SuppressWarnings("deprecation") 
ActionBarDrawerToggle mDrawerToggle; 
String mTitle=""; 
private final String TAG = this.getClass().getSimpleName(); 

    LocationRequest mLocationRequest; 
    GoogleApiClient mGoogleApiClient; 

    LatLng latLng; 
    GoogleMap mMap; 
    SupportMapFragment mFragment; 
    Marker CurrentMarker,NearbyPlace,FindMarker; 
    EditText editplace = null; 
    Button findbtn = null; 
    boolean firstRun = true; 
    private Boolean exit = false; 


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

    //for connectivity service 
    ConnectivityManager cManager = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE); 
    NetworkInfo nInfo = cManager.getActiveNetworkInfo(); 
    if (nInfo != null && nInfo.isConnected()) { 
     Toast.makeText(this, "Network is available ", Toast.LENGTH_SHORT).show(); 

     AdView adView = (AdView)findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder() 
     //.addTestDevice("abc") 
     .build(); 
     adView.loadAd(adRequest); 
    mFragment=(SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
    mFragment.getMapAsync(this); 

    } 


    else{ 
     Toast.makeText(this, "Network is not available ", Toast.LENGTH_SHORT).show(); 
     Fragment newFragment; 
     FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
     newFragment = new NetworkCheck(); 
     transaction.replace(R.id.content_frame, newFragment); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 

    editplace = (EditText) findViewById(R.id.editplace); 
    findbtn = (Button) findViewById(R.id.findbtn); 

    setListnerOnWidget(); 

    mTitle = (String) getTitle(); 
    // Getting reference to the DrawerLayout 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
    mDrawerList = (ListView) findViewById(R.id.drawer_list); 

    // Getting reference to the ActionBarDrawerToggle 
    mDrawerToggle = new ActionBarDrawerToggle(this, 
     mDrawerLayout, 
     R.drawable.ic_drawer, 
     R.string.drawer_open, 
     R.string.drawer_close){ 

      /** Called when drawer is closed */ 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
       invalidateOptionsMenu(); 
      } 

      /** Called when a drawer is opened */ 
      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle("Select a menu"); 
       invalidateOptionsMenu(); 
      } 
    }; 

    // Setting DrawerToggle on DrawerLayout 
    mDrawerLayout.setDrawerListener(mDrawerToggle); 
    mDrawerToggle.setDrawerIndicatorEnabled(true); 

    // Creating an ArrayAdapter to add items to the listview mDrawerList 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
     getBaseContext(), 
     R.layout.drawer_list_item , 
     getResources().getStringArray(R.array.menu) 
    ); 

    mDrawerList.setAdapter(adapter); 

    getActionBar().setHomeButtonEnabled(true); 
    getActionBar().setDisplayHomeAsUpEnabled(true); 


    // Setting item click listener for the listview mDrawerList 
    mDrawerList.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, 
      View view, 
      int position, 
      long id) { 
      displayView(position); 
      // Updating the action bar title 
      String[] menu = getResources().getStringArray(R.array.menu); 
      mTitle = menu[position]; 

      mDrawerLayout.closeDrawer(mDrawerList); 

     } 

     private void displayView(int position) { 
      // TODO Auto-generated method stub 
      Fragment newFragment; 
      FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
      switch (position) { 
      case 0: 

       newFragment = new Help(); 
       transaction.replace(R.id.content_frame, newFragment); 
       transaction.addToBackStack(null); 
       transaction.commit(); 
       Log.e(TAG, "help button is clicked"); 
       break; 

      case 1: 

       newFragment = new ContactUs(); 
       transaction.replace(R.id.content_frame, newFragment); 
       transaction.addToBackStack(null); 
       transaction.commit(); 
       Log.e(TAG, "ContactUs button is clicked"); 
       break; 

       case 2: 
        transaction.addToBackStack(null); 
       try 
       { Intent i = new Intent(Intent.ACTION_SEND); 
        i.setType("text/plain"); 
        i.putExtra(Intent.EXTRA_SUBJECT, "My application name"); 
        String sAux = "\nLet me recommend you this application\n\n"; 
        sAux = sAux + "https://play.google.com/store/apps/details?id=com.iqwiner\n\n"; 
        i.putExtra(Intent.EXTRA_TEXT, sAux); 
        startActivity(Intent.createChooser(i, "Choose an action")); 

       } 
       catch(Exception e) 
       { //e.toString(); 
       } 
       Log.e(TAG, "share button is clicked"); 
       break; 

       case 3: 
       newFragment = new Suggestion_Address(); 
       transaction.replace(R.id.content_frame, newFragment); 
       transaction.addToBackStack(null); 
       transaction.commit(); 
       Log.e(TAG, "Suggestion Address button is clicked"); 
       break; 

      } 

     } 

    }); 



} 

方法についてはをクリックしたときに、フラグメントレイアウトから私のメインのレイアウトに戻る方法を教えてください。 [戻る]ボタン..

おかげ

答えて

0

@Override 
public void onBackPressed() { 
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) { 
    getSupportFragmentManager().popBackStackImmediate(); 
    } else { 
    super.onBackPressed(); 
    } 
} 
0

あなたの活動でこれを試してみてくださいここで断片化する活動からナビゲートしたコードであり、それはナビゲーションボタンを押してバックに活動に戻る。

MainActivity to fragment and back to MainActivity on pressing back button

+0

はありません、私は戻ってあなたがコードを確認し、それに応じて変更を加える必要があり –

+0

フラグメントからの主な活動にしたいです。コピー - コードを貼ることはあなたを助けません。 –

関連する問題