Back to project page MySms.
The source code is released under:
Apache License
If you think the Android project MySms listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.henningta.mysms; // w w w. j a v a2 s .c o m import android.app.Fragment; import android.app.FragmentManager; import android.content.Context; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.inputmethod.InputMethodManager; public class MainActivity extends ActionBarActivity implements ConversationsFragment.OnSourceSelected { private FragmentManager fragmentManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // initialize toolbar Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar); // init fragment manager fragmentManager = getFragmentManager(); fragmentManager.addOnBackStackChangedListener(backStackChangedListener); if (savedInstanceState == null) { loadConversationsFragment(); String source = getIntent().getStringExtra(Settings.KEY_SOURCE); if (source != null) { loadMessagesFragment(source); } } shouldDisplayHomeUp(); } private void loadConversationsFragment() { String tag = ConversationsFragment.TAG; Fragment fragment = fragmentManager.findFragmentByTag(tag); if (fragment == null) { fragment = ConversationsFragment.newInstance(); } // replace existing fragment fragmentManager.beginTransaction() .replace(R.id.content_frame, fragment, tag) .commit(); } private void loadMessagesFragment(String source) { String tag = MessagesFragment.TAG + "." + source; Fragment fragment = fragmentManager.findFragmentByTag(tag); if (fragment == null) { fragment = MessagesFragment.newInstance(source); } // replace existing fragment fragmentManager.beginTransaction() .add(R.id.content_frame, fragment, tag) .addToBackStack(null) .commit(); } @Override public void onSourceSelected(String source) { loadMessagesFragment(source); } @Override public void onBackPressed() { if (fragmentManager.getBackStackEntryCount() == 0) { super.onBackPressed(); } else { fragmentManager.popBackStack(); } } private FragmentManager.OnBackStackChangedListener backStackChangedListener = new FragmentManager.OnBackStackChangedListener() { @Override public void onBackStackChanged() { shouldDisplayHomeUp(); // check that we're on the conversation fragment if (fragmentManager.getBackStackEntryCount() == 0) { // set toolbar getSupportActionBar().setTitle(R.string.app_name); getSupportActionBar().setSubtitle(null); // hide keyboard hideKeyboard(); } } }; @Override public boolean onSupportNavigateUp() { fragmentManager.popBackStack(); return true; } private void shouldDisplayHomeUp() { //Enable Up button only if there are entries in the back stack boolean hasBack = fragmentManager.getBackStackEntryCount() > 0; getSupportActionBar().setDisplayHomeAsUpEnabled(hasBack); } private void hideKeyboard() { InputMethodManager inputManager = (InputMethodManager)this.getSystemService( Context.INPUT_METHOD_SERVICE); // check if no view has focus: View v = this.getCurrentFocus(); if (v == null) { return; } inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0); } }