Created
March 2, 2016 19:41
-
-
Save MrOnyszko/670633c2f2ac15e78f66 to your computer and use it in GitHub Desktop.
ActivityHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
/** | |
* Created on 05.02.2016 | |
* | |
* @author Sławomir Onyszko | |
*/ | |
public class ActivityHelper { | |
/** | |
* Log tag. | |
*/ | |
private static final String TAG = ActivityHelper.class.getSimpleName(); | |
/** | |
* Starts an activity with flags: | |
* <p/> | |
* {@link Intent#FLAG_ACTIVITY_SINGLE_TOP} | |
* {@link Intent#FLAG_ACTIVITY_NO_HISTORY} | |
* {@link Intent#FLAG_ACTIVITY_NEW_TASK} | |
* {@link Intent#FLAG_ACTIVITY_CLEAR_TASK} | |
* | |
* @param from an activity starts from. | |
* @param to type of activity which will be started. | |
*/ | |
public static void startActivityWithNoHistory(Activity from, Class<?> to) { | |
Intent intent = new Intent(from, to); | |
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
from.startActivity(intent); | |
} | |
/** | |
* Starts specified activity. | |
* | |
* @param from an activity starts from. | |
* @param to type of activity which will be started. | |
*/ | |
public static void startActivity(Activity from, Class<?> to) { | |
Intent intent = new Intent(from, to); | |
from.startActivity(intent); | |
} | |
/** | |
* Sets toolbar with up navigation. | |
* | |
* @param activity an activity which toolbar will set on. | |
* @param toolbar {@link Toolbar} | |
*/ | |
public static void setupToolbar(AppCompatActivity activity, Toolbar toolbar) { | |
try { | |
activity.setSupportActionBar(toolbar); | |
//noinspection ConstantConditions | |
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true); | |
} catch (Exception e) { | |
Log.e(TAG, e.getMessage(), e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment