Last active
August 26, 2017 17:38
-
-
Save GabriellCosta/98b65e005756c4b6fa7a8f95104e40dd 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 i.gabrielcosta.customstartexample; | |
import android.content.Context; | |
import java.io.Serializable; | |
/** | |
* Created by gabrielcosta on 26/08/17. | |
*/ | |
public interface Executor extends Serializable { | |
void execute(final Context context); | |
} |
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 i.gabrielcosta.customstartexample | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import android.widget.Button | |
import android.widget.Toast | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val button = Button(this) | |
button.text = "Open Normal Activity" | |
setContentView(button) | |
button.setOnClickListener { | |
openNormalActivity() | |
} | |
} | |
private fun openNormalActivity() { | |
startNormalActivity(baseContext, Executor { context -> | |
Toast.makeText(context, "Executado", Toast.LENGTH_LONG) | |
.show() | |
}) | |
} | |
} |
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 i.gabrielcosta.customstartexample | |
import android.content.Context | |
import android.content.Intent | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
fun startNormalActivity(context: Context, executor: Executor) { | |
val intent = Intent(context, NormalActivity::class.java) | |
intent.putExtra(NormalActivity.EXTRA_EXECUTOR, executor) | |
context.startActivity(intent) | |
} | |
class NormalActivity : AppCompatActivity() { | |
companion object { | |
val EXTRA_EXECUTOR = "EXTRA_EXECUTOR" | |
} | |
lateinit var executor: Executor | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_normal) | |
extractExecutor() | |
} | |
private fun extractExecutor() { | |
if (intent.hasExtra(EXTRA_EXECUTOR)) { | |
executor = intent.getSerializableExtra(EXTRA_EXECUTOR) as Executor | |
executor.execute(this) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing example.