How To Move Activity Form Fragment "Kotlin" - android

How do you move the Activity from the Fragment? using Kotlin
btnGlobal.setOnClickListener {
val intent = Intent (getActivity(), Main2Activity::class.java)
getActivity()?.startActivity(intent)
}`

Probably you are willing to go to another activity named Main2Activity from your current activity. If it is then the process is as follows:
btnGlobal.setOnClickListener {
val intent = Intent (requireActivity(), Main2Activity::class.java)
startActivity(intent)
// For finishing current activity use below line
// requireActivity().finish()
}

If you are in a fragment do the following:
btnGlobal.setOnClickListener {
val intent = Intent (activity!!, Main2Activity::class.java)
startActivity(intent)
}
If you are on an Activity do the following:
btnGlobal.setOnClickListener {
val intent = Intent (TheNameOfThisActivity#this, Main2Activity::class.java)
startActivity(intent)
}

Related

How to check if the user's login details are still in the firebase when opening the app

I am trying to check if user's details are still in firebase when opening the app. If so he should move to the main activity else move to the login activity.
if(auth.currentUser != null){
Handler().postDelayed({
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}, 3000)
}else{
Handler().postDelayed({
val intent = Intent(this, SignUp::class.java)
startActivity(intent)
finish()
},3000)}

Intent Activity Utils , not declarated in Manifest - Kotlin, Android

I am trying to achieve Intent Activity Utils as extended functions in Kotlin. They will start activities from other Activity/Fragment. Please suggest to me the best way to do that().
I tried to solve that problem like that:
In Activity I invoke extend functions like that :
when (view.id) {
PROFILE.actionViewId -> {
// Start Activity normally
startNewActivity(MyProfileActivity::class.java)
}
PREPAID_RECHARGE.actionViewId -> {
// Dont start activity and crash
startNewActivityForResult(RechargePrepaidActivity::class.java, RECHARGE_PREPAID_REQUEST_CODE)
// Start Activity normally
this.startActivityForResult(Intent(this, RechargePrepaidActivity::class.java), RECHARGE_PREPAID_REQUEST_CODE)
}
TROUBLE_TICKETS.actionViewId -> {
startNewActivity(TroubleTicketsActivity::class.java) // Start Activity normally
}
HELP.actionViewId -> {
startNewActivity(HelpSectionActivity::class.java) // Start Activity normally
}
IntentUtils.kt:
fun <T : BaseActivity> BaseActivity.startNewActivity(newActivity: Class<T>) {
val intent = Intent(this, newActivity)
this.startActivity(intent)
}
//fun BaseActivity.startNewActivity(newActivity: Class<*>) {
// val intent = Intent(this, newActivity)
// this.startActivity(intent)
//}
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity::class.java)
startActivityForResult(intent, requestCode)
}
fun BaseFragment.startNewActivity(newActivity: Class<*>) {
val intent = Intent(this.requireActivity(), newActivity::class.java)
this.startActivity(intent)
}
fun BaseFragment.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this.requireActivity(), newActivity::class.java)
startActivityForResult(intent, requestCode)
}
and also, the Activity is declared in the Manifest - Kotlin, Android
<activity
android:name=".ui.recharge.RechargePrepaidActivity"
android:configChanges="locale|orientation|screenSize"
android:screenOrientation="portrait" />
This starts activity normally:
startNewActivity(MyProfileActivity::class.java) // use extend func
startActivity(Intent(this, MyProfileActivity::class.java)) // use inherited Activity func
But when I try to create the same function for starting activity for result via request code it doesn't work, when I use inherited Activity function startActivityForResult() it's ok
// Dont start activity and crash
startNewActivityForResult(RechargePrepaidActivity::class.java, RECHARGE_PREPAID_REQUEST_CODE)
// Start Activity normally
startActivistyForResult(Intent(this, RechargePrepaidActivity::class.java), RECHARGE_PREPAID_REQUEST_CODE)
The strange here is that when I use my IntentUtils.startNewActivityForResult() app crashes with
ActivityNotFoundException, but on normal use of Fragment.startActivityForResult() it doesn't have this behaviour:
Caused by: android.content.ActivityNotFoundException:
Unable to find explicit activity class {/java.lang.Class}; have you declared this activity in your AndroidManifest.xml?
at ui.utils.view.IntentUtilsKt.startNewActivityForResult(IntentUtils.kt:19)
at ui.main.hamburgerMenu.HamburgerMenuActivity.onClick(HamburgerMenuActivity.kt:117)
The problem was in the duplicated ::class.java invocation :
WRONG:
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity::class.java) //duplicated ::class.java
startActivityForResult(intent, requestCode)
}
RIGHT:
fun BaseActivity.startNewActivityForResult(newActivity: Class<*>, requestCode : Int) {
val intent = Intent(this, newActivity) // removed ::class.java
startActivityForResult(intent, requestCode)
}

Android: Fragment is not receiving RESULT_OK in Activity result contract

I am using ActivityResultContract API in my fragment. Fragment A is in Activity A, from where Activity B is called which has Fragment B. Code in Fragment A is as follows:
val makePaymentContract = registerForActivityResult(CustomContract()) { uri: Uri? ->
Log.d("FragmentA", "DATA URI: ${uri.toString()}")
}
This is custom contract:
class CustomContract : ActivityResultContract<DataHolder, Uri>() {
override fun createIntent(context: Context, input: DataHolder?): Intent {
val intent = Intent(context, ActivityB::class.java)
intent.putExtra("data1", input?.data1)
intent.putExtra("data2", input?.data2)
intent.putExtra("data3", input?.data3)
intent.putExtra("data4", input?.data4)
return intent
}
override fun parseResult(resultCode: Int, intent: Intent?): Uri? {
Log.d("CustomContract", "Result Code $resultCode")
when (resultCode) {
Activity.RESULT_OK -> return intent?.data // Return the data
else -> return null
}
}
}
where DataHolder is a simple data class. Below code is in Fragment B(which is in Activity B)
Uri uri = Uri.parse("custom://thank_you?id=" + id + "&amt_paid=" +
totalPaidAmt + "&order_type=" + orderType);
Intent intent = new Intent();
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
requireActivity().finish();
The problem is FragmentA always receives null uri. Upon debugging, found out returned resultCode is always RESULT_CANCELED. What am I doing wrong?
Found out the mistake. I was not setting result from Fragment B
Uri uri = Uri.parse("custom://thank_you?id=" + id + "&amt_paid=" +
totalPaidAmt + "&order_type=" + orderType);
Intent intent = new Intent();
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
requireActivity().setResult(Activity.RESULT_OK, intent);
requireActivity().finish();

Getting a Null value while passing data through intent to another activity

When I do a debug, I can see the first activity sending thecorrect number, but the second activity always gets a null value. Thank you for helping.
First Activity:
val intent = Intent(this#InfBateria,ControlActivity::class.java)
intent.putExtra(EXTRA_PERCENTAGE,batteryPercentage.toString())
Second Activity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.control_layout)
m_address = intent.getStringExtra(Connect.EXTRA_ADDRESS)
val m_percentage: String? = intent.getStringExtra(InfBateria.EXTRA_PERCENTAGE)
textView9.text = m_percentage
[Edit] Code of the Activity (differente from the First one) that acttualy opens da Second Activity:
val intent = Intent(this,ControlActivity::class.java)
intent.putExtra(EXTRA_ADDRESS,address)
startActivity(intent)
If you don't do anything with this result intent, it will not do anything:
val intent = Intent(this#InfBateria,ControlActivity::class.java)
intent.putExtra(EXTRA_PERCENTAGE,batteryPercentage.toString())
You need to update the intent that you are sending to the second activity, like so:
val intent = Intent(this, ControlActivity::class.java)
intent.putExtra(EXTRA_ADDRESS, address)
intent.putExtra(EXTRA_PERCENTAGE, batteryPercentage.toString())
startActivity(intent)
Then your receiving activity will get both the extras. :-)
If you have Three activities and want to get data from 1st to 3rd you do it like this:
First Activity:
val intent = Intent(this, SecondActivity::class.java)
intent.putExtra(ThirdActivity.EXTRA_PERCENTAGE, batteryPercentage.toString())
startActivity(intent)
Second Activity:
val intent = Intent(this, ThirdActivity::class.java)
intent.putExtra(ThirdActivity.EXTRA_ADDRESS, address)
// Get the data you sent to this activity and re-add it to send again
intent.putExtra(ThirdActivity.EXTRA_PERCENTAGE, getIntent().getStringExtra(ThirdActivity.EXTRA_PERCENTAGE)
startActivity(intent)
Third Activity:
m_address = intent.getStringExtra(EXTRA_ADDRESS)
m_battery = intent.getStringExtra(EXTRA_PERCENTAGE)

how pass data between more than 2 activities Kotlin

I need to pass multiple variables between more than two activities.
I know how to pass between one to another, but not from the first to the third.
Activity One
btnNext.setOnClickListener {
val i = Intent(this, Activity2::class.java)
val variableOne= txtVariableOne.text.toString()
i.putExtra("VARIABLEONE",variableOne)
startActivity(i)
}
Activity TWO
btnNext.setOnClickListener {
val i = Intent(this, Activity3::class.java)
val VariableTwo = txtVariableTwo.text.toString()
i.putExtra("VARIABLETWO",variableTwo)
startActivity(i)
}
Activity THREE (This is what I need)
Log.d("DEBUG ", "Value: " + intent.getStringExtra("VARIABLEONE"))
Log.d("DEBUG ", "Value: " + intent.getStringExtra("VARIABLETWO"))
I need access VariableOne and VariableTwo from activity Three.
Thank
Activity TWO
btnNext.setOnClickListener {
val i = Intent(this, Activity3::class.java)
val VariableTwo = txtVariableTwo.text.toString()
//THIS WHAT YOU NEED:
i.putExtra("VARIABLEONE",intent.getStringExtra("VARIABLEONE"));
i.putExtra("VARIABLETWO",variableTwo)
startActivity(i)
}
Activity Two
override fun viewAllNotes() {
val intent : Intent = Intent(this, ViewAllNotesActivity::class.java)
intent .putExtra("VARIABLEONE",variableOne)
startActivityForResult(intent, VIEW_ALL_REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK){
if (requestCode == VIEW_ALL_REQUEST_CODE){
//get data here
}
}
}
}
Activity three
#Override
public void onBackPressed() {
Intent result = new Intent();
result .putExtra("VARIABLEONE",variableOne)
setResult(Activity.RESULT_OK, result);
this.finish();
}
Or else you can store the values using Shared-preference and get values any where in the project
I can say the same as we passing in java
There are many ways
From the one is, You can pass by intent when starting another activity which is proper when you think data at the time of starting the activity like bellow,
val intent = Intent(mActivity, LoginActivity::class.java);
intent.putExtra("keyName", data)
startActivity(intent)
Another way you can use preference or store data in preference and you can use where ever you need.

Categories

Resources