|  | 
|  | 1 | +package com.appboy.segment.appboysample; | 
|  | 2 | + | 
|  | 3 | +import android.content.Context; | 
|  | 4 | +import android.support.v4.app.Fragment; | 
|  | 5 | +import android.support.v4.app.FragmentManager; | 
|  | 6 | +import android.support.v4.app.FragmentTransaction; | 
|  | 7 | +import android.os.Bundle; | 
|  | 8 | +import android.util.Log; | 
|  | 9 | +import android.support.v7.app.AppCompatActivity; | 
|  | 10 | +import android.support.v7.widget.Toolbar; | 
|  | 11 | +import android.view.Menu; | 
|  | 12 | +import android.view.MenuItem; | 
|  | 13 | +import android.view.inputmethod.InputMethodManager; | 
|  | 14 | +import android.widget.Toast; | 
|  | 15 | + | 
|  | 16 | +import com.appboy.Constants; | 
|  | 17 | +import com.appboy.support.AppboyLogger; | 
|  | 18 | +import com.appboy.ui.AppboyFeedFragment; | 
|  | 19 | + | 
|  | 20 | +public class MainActivity extends AppCompatActivity { | 
|  | 21 | +  private static final String TAG = String.format("%s.%s", Constants.APPBOY_LOG_TAG_PREFIX, MainActivity.class.getName()); | 
|  | 22 | + | 
|  | 23 | +  @Override | 
|  | 24 | +  protected void onCreate(Bundle savedInstanceState) { | 
|  | 25 | +    super.onCreate(savedInstanceState); | 
|  | 26 | + | 
|  | 27 | +    AppboyLogger.setLogLevel(Log.VERBOSE); | 
|  | 28 | +    setContentView(R.layout.activity_main); | 
|  | 29 | +    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | 
|  | 30 | +    setSupportActionBar(toolbar); | 
|  | 31 | + | 
|  | 32 | +    final FragmentManager fragmentManager = getSupportFragmentManager(); | 
|  | 33 | +    Fragment currentFragment = fragmentManager.findFragmentById(R.id.root); | 
|  | 34 | + | 
|  | 35 | +    if (currentFragment == null) { | 
|  | 36 | +      fragmentManager.beginTransaction().add(R.id.root, new MainFragment()).commit(); | 
|  | 37 | +    } | 
|  | 38 | +  } | 
|  | 39 | + | 
|  | 40 | +  @Override | 
|  | 41 | +  public boolean onCreateOptionsMenu(Menu menu) { | 
|  | 42 | +    // Inflate the menu; this adds items to the action bar if it is present. | 
|  | 43 | +    getMenuInflater().inflate(R.menu.menu_main, menu); | 
|  | 44 | +    return true; | 
|  | 45 | +  } | 
|  | 46 | + | 
|  | 47 | +  @Override | 
|  | 48 | +  public boolean onOptionsItemSelected(MenuItem item) { | 
|  | 49 | +    if (!AppboySegmentApplication.sAppboySegmentEnabled) { | 
|  | 50 | +      Toast.makeText(this, "Appboy integration disabled. Doing nothing.", Toast.LENGTH_LONG).show(); | 
|  | 51 | +      return super.onOptionsItemSelected(item); | 
|  | 52 | +    } | 
|  | 53 | +    // Handle action bar item clicks here. The action bar will | 
|  | 54 | +    // automatically handle clicks on the Home/Up button, so long | 
|  | 55 | +    // as you specify a parent activity in AndroidManifest.xml. | 
|  | 56 | +    int id = item.getItemId(); | 
|  | 57 | +    if (id == R.id.newsfeed) { | 
|  | 58 | +      replaceCurrentFragment(new AppboyFeedFragment()); | 
|  | 59 | +      return true; | 
|  | 60 | +    } | 
|  | 61 | +    return super.onOptionsItemSelected(item); | 
|  | 62 | +  } | 
|  | 63 | + | 
|  | 64 | +  private void replaceCurrentFragment(Fragment newFragment) { | 
|  | 65 | +    FragmentManager fragmentManager = getSupportFragmentManager(); | 
|  | 66 | +    Fragment currentFragment = fragmentManager.findFragmentById(R.id.root); | 
|  | 67 | + | 
|  | 68 | +    if (currentFragment != null && currentFragment.getClass().equals(newFragment.getClass())) { | 
|  | 69 | +      Log.i(TAG, String.format("Fragment of type %s is already the active fragment. Ignoring request to replace " + | 
|  | 70 | +          "current fragment.", currentFragment.getClass())); | 
|  | 71 | +      return; | 
|  | 72 | +    } | 
|  | 73 | + | 
|  | 74 | +    hideSoftKeyboard(); | 
|  | 75 | +    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); | 
|  | 76 | +    fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out, | 
|  | 77 | +        android.R.anim.fade_in, android.R.anim.fade_out); | 
|  | 78 | +    fragmentTransaction.replace(R.id.root, newFragment, newFragment.getClass().toString()); | 
|  | 79 | +    if (currentFragment != null) { | 
|  | 80 | +      fragmentTransaction.addToBackStack(newFragment.getClass().toString()); | 
|  | 81 | +    } else { | 
|  | 82 | +      fragmentTransaction.addToBackStack(null); | 
|  | 83 | +    } | 
|  | 84 | +    fragmentTransaction.commit(); | 
|  | 85 | +  } | 
|  | 86 | + | 
|  | 87 | +  private void hideSoftKeyboard() { | 
|  | 88 | +    if (getCurrentFocus() != null) { | 
|  | 89 | +      InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); | 
|  | 90 | +      inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), | 
|  | 91 | +          InputMethodManager.RESULT_UNCHANGED_SHOWN); | 
|  | 92 | +    } | 
|  | 93 | +  } | 
|  | 94 | +} | 
0 commit comments