Created
December 24, 2016 19:04
-
-
Save Golgorz/6d0461b83369d1d67439a700906b5091 to your computer and use it in GitHub Desktop.
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
package com.golgorz.skeletor.skhelpers; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.os.Parcelable; | |
import com.google.gson.Gson; | |
import java.io.Serializable; | |
/** | |
* Created by Diego on 03/11/2016. | |
*/ | |
public class SkIntent extends Intent { | |
private Context context; | |
private Intent goToIntent; | |
public static SkIntent with(Context context) { | |
return new SkIntent(context); | |
} | |
private SkIntent(Context context) { | |
goToIntent = new Intent(); | |
this.context = context; | |
} | |
public SkIntent addNewTask() { | |
this.withFlag(Intent.FLAG_ACTIVITY_NEW_TASK); | |
return this; | |
} | |
public SkIntent addClearTask() { | |
this.withFlag(Intent.FLAG_ACTIVITY_CLEAR_TASK); | |
return this; | |
} | |
public SkIntent goTo(Class goToClass){ | |
goToIntent.setClass(context, goToClass); | |
return this; | |
} | |
public void goToTop(Class goToClass){ | |
goToIntent.setClass(context, goToClass); | |
this.addNewTask().addClearTask(); | |
this.go(); | |
} | |
public SkIntent withData(String key, boolean value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, String value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, float value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, double value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, long value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, Parcelable value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, Serializable value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, Object value) { | |
Gson gson = new Gson(); | |
goToIntent.putExtra(key, gson.toJson(value)); | |
return this; | |
} | |
public SkIntent withData(String key, Bundle value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, short value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, char value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withData(String key, CharSequence value) { | |
goToIntent.putExtra(key, value); | |
return this; | |
} | |
public SkIntent withFlag(int flag) { | |
goToIntent.addFlags(flag); | |
return this; | |
} | |
public void go() { | |
context.startActivity(goToIntent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment