I have two activities A and B. A is the homepage and when the plus button is clicked it goes to B where there are two edit texts (question and answer). When both are inputted it and the check button is clicked, it is taken back to A where a new button appears with the text set to the inputted question. I have an inflated view which allows the user to edit the question and answer when clicking the button. The problem is that when I try to use intent to go back to activity B, the previously inputted texts are gone. I tried to use onActivityResult but its not working. Does anyone have any insight?
UPDATE:
I'm going to put my updated code below. I figured out how to send inputs between each activity and now each dynamically created button saved the individual pairs of edit texts. The problem I'm running across now is that it only saves the edit texts once and doesn't allow me to change it again. I've tried bundles, intent and shared preferences but somehow none of them have been working. I thought that having my intents at the top would allow me to save my data every time I inputted text but it isn't working. Here is the code I've been using:
Activity A:
package com.example.yesh
import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private val questionActivityCode = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<FloatingActionButton>(R.id.btn2).setOnClickListener{
startActivityForResult(Intent(this#MainActivity, SecondActivity::class.java), questionActivityCode)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == questionActivityCode && resultCode == Activity.RESULT_OK) {
createNewButtonWithText(data?.getStringExtra("test") ?: "", data?.getStringExtra("test2") ?: "")
}
}
private fun createNewButtonWithText(text: String, s: String)
{
val newbutton = Button(this#MainActivity)
val layout = findViewById<LinearLayout>(R.id.mainlayout)
newbutton.text = text
newbutton.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
newbutton.width=1010
newbutton.height=300
newbutton.gravity = Gravity.CENTER
newbutton.translationX= 65F
newbutton.setTextColor(Color.parseColor("#FFFFFFFF"))
newbutton.setBackgroundColor(Color.parseColor("#250A43"))
layout.addView(newbutton)
val inflator = layoutInflater
val builder = AlertDialog.Builder(this)
val intent1 = Intent(this#MainActivity, SecondActivity::class.java)
intent1.putExtra("test", text)
intent1.putExtra("test2", s)
newbutton.setOnClickListener{
val dialogLayout = inflator.inflate(R.layout.text, null)
with(builder) {
setTitle(newbutton.text)
setPositiveButton("Edit"){dialog, which ->
startActivity(intent1)
}
setNegativeButton("Delete"){dialog, which ->
layout.removeView(newbutton)
}
setView(dialogLayout)
show()
}
}
}
}
Activity B:
package com.example.yesh
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val question = findViewById<EditText>(R.id.question)
val answer = findViewById<EditText>(R.id.answer)
val value = intent.getStringExtra("test")
question.setText(value)
val value2 = intent.getStringExtra("test2")
answer.setText(value2)
findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener {
val questiontext = question.text.toString()
val answertext = answer.text.toString()
saveData()
val returnIntent = Intent()
returnIntent.putExtra("test", questiontext)
returnIntent.putExtra("test2", answertext)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
}
private fun saveData() {
val question = findViewById<EditText>(R.id.question)
val answer = findViewById<EditText>(R.id.answer)
val txt = intent.getStringExtra("test2")
answer.setText(txt)
Toast.makeText(this, "Data saved", Toast.LENGTH_SHORT).show()
}
}
remember finish() in the B activity? It destroyed that activity already so there is not way to going back to it again, when you press the new button in activity A it and call
startActivityForResult(intent, 1) you are creating new instance of that activity , this new instance doesn't know about one which is already destroyed so you should also send the data in the intent which you want to display
I agree with Abhinav's answer, You cannot access an activity you destroyed with "finish()" as it removes the activity from memory.
The correct way of using onActivityResult() is for cases like fetching data from another activity.
For example, you have a form in Activity A and and there is a field called location. for that you can open Google maps in Activity B and when it is selected, you can return the location in activity A and display in already existing view in A.
Related
I'm trying to make a quiz app so that when the user clicks the plus button on the home page it takes them to a page where they input a question and answer and click the check mark and is taken back to the homepage where a new button with the text set to the question is created. I have all of this coded and working except for the part where I can save the previously inputted question/answer activity state if the user wants to edit it with my layout inflator option. However, I have no idea how to have the inputted question and answer saved when starting the intent for the second activity on the homepage. I tried intent flags but they don't work. Whenever I test my application and click edit, it goes to a new second activity and previously inputted text is gone. Can someone please help? Here's my code:
Second Activity:
package com.example.quest
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.google.android.material.floatingactionbutton.FloatingActionButton
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val question = findViewById<EditText>(R.id.question)
val answer = findViewById<EditText>(R.id.answer)
findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener {
val questiontext = question.text.toString()
val answertext = answer.text.toString()
val sharedPre = PreferenceManager.getDefaultSharedPreferences(applicationContext).edit()
sharedPre.putString("question", questiontext)
sharedPre.putString("answer", answertext)
sharedPre.apply()
val returnIntent = Intent()
returnIntent.putExtra("test", questiontext)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
}
}
Main Activity:
package com.example.quest
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import androidx.preference.PreferenceManager
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private val questionActivityCode = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<FloatingActionButton>(R.id.btn2).setOnClickListener{
startActivityForResult(Intent(this#MainActivity, SecondActivity::class.java), questionActivityCode)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == questionActivityCode && resultCode == Activity.RESULT_OK) {
createNewButtonWithText(data?.getStringExtra("test") ?: "")
}
}
private fun createNewButtonWithText(text: String)
{
val newbutton = Button(this#MainActivity)
val layout = findViewById<LinearLayout>(R.id.mainlayout)
newbutton.text = text
newbutton.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
newbutton.width=1010
newbutton.height=300
newbutton.gravity = Gravity.CENTER
newbutton.translationX= 65F
newbutton.setTextColor(Color.parseColor("#FFFFFFFF"))
newbutton.setBackgroundColor(Color.parseColor("#250A43"))
layout.addView(newbutton)
val inflator = layoutInflater
val builder = AlertDialog.Builder(this)
val intent = Intent(this#MainActivity, SecondActivity::class.java)
newbutton.setOnClickListener{
val dialogLayout = inflator.inflate(R.layout.text, null)
with(builder) {
setTitle(newbutton.text)
setPositiveButton("Edit"){dialog, which ->
startActivity(intent)
val loadSharedPre = PreferenceManager.getDefaultSharedPreferences(applicationContext)
loadSharedPre.all
}
setNegativeButton("Cancel"){dialog, which ->
Log.d("Main", "Negative button clicked")
}
setView(dialogLayout)
show()
}
}
}}
I have two activities. One is the homepage with a button to add a dynamic button and the second page is two edit texts. So you click the button on the homepage and go to the second page where you input two edit texts and youre sent back to the homepage where a dynamic button set to one of the edit text is created. I made an inflator that allows you to go back into the second activity through the button but it starts a new second activity so the previous inputted edit texts are gone. How do I get them to stay?
Main page:
package com.example.quest
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import androidx.preference.PreferenceManager
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private val questionActivityCode = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<FloatingActionButton>(R.id.btn2).setOnClickListener{
startActivityForResult(Intent(this#MainActivity, SecondActivity::class.java), questionActivityCode)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == questionActivityCode && resultCode == Activity.RESULT_OK) {
createNewButtonWithText(data?.getStringExtra("test") ?: "")
}
}
private fun createNewButtonWithText(text: String)
{
val newbutton = Button(this#MainActivity)
val layout = findViewById<LinearLayout>(R.id.mainlayout)
newbutton.text = text
newbutton.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
newbutton.width=1010
newbutton.height=300
newbutton.gravity = Gravity.CENTER
newbutton.translationX= 65F
newbutton.setTextColor(Color.parseColor("#FFFFFFFF"))
newbutton.setBackgroundColor(Color.parseColor("#250A43"))
layout.addView(newbutton)
val inflator = layoutInflater
val builder = AlertDialog.Builder(this)
val intent = Intent(this#MainActivity, SecondActivity::class.java)
newbutton.setOnClickListener{
val dialogLayout = inflator.inflate(R.layout.text, null)
with(builder) {
setTitle(newbutton.text)
setPositiveButton("Edit"){dialog, which ->
startActivity(intent)
}
setNegativeButton("Cancel"){dialog, which ->
Log.d("Main", "Negative button clicked")
}
setView(dialogLayout)
show()
}
}
}}
Second Activity:
package com.example.quest
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import com.google.android.material.floatingactionbutton.FloatingActionButton
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val question = findViewById<EditText>(R.id.question)
val answer = findViewById<EditText>(R.id.answer)
findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener {
val questiontext = question.text.toString()
val answertext = answer.text.toString()
val sharedPre = PreferenceManager.getDefaultSharedPreferences(this#SecondActivity).edit()
sharedPre.putString("question", questiontext)
sharedPre.putString("answer", answertext)
sharedPre.apply()
val returnIntent = Intent()
returnIntent.putExtra("test", questiontext)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
}
}
If you look at the activity lifecycle after creating an activity and when moving from one activity to the next activity the activity is destroyed, and a new activity is created. An easy way to save this data could be to use shared preferences whereby you can store the data when the user inputs it in the second activity and before you inflate the second activity check if the sharedprefs data is not null and use the data to update the activity else just create a new activity.
Are you using MVVM architecture? You should probably store all your value in ViewModel and call these values between activities in ViewModel, as View (Actiity/Fragment) is only responsible for UI-related task.
Here is a simple example
SharedViewModel
class SharedViewModel : ViewModel() {
// ViewModel that holds data-related values
var question: String ?= null
var answer: Stirng ?= null
}
SecondActivity
class SecondActivity : AppCompatActivity() {
private val sharedViewModel: SharedViewModel by viewModels() <-- add your viewmodel here
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val question = findViewById<EditText>(R.id.question)
val answer = findViewById<EditText>(R.id.answer)
// Step 1 - Check viewmodel's value and assign it back to edittext
if(sharedViewModel.question != null) {
question.text = sharedViewModel.question
}
if(sharedViewModel.answer != null) {
answer.text = sharedViewModel.answer
}
// Step 2 - Assign your value into viewmodel
findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener {
sharedViewModel.question = question.text.toString()
sharedViewModel.answer = answer.text.toString()
returnIntent.putExtra("test", questiontext)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
}
}
Additional
onActivityResult will be deprecated soon in API 30, it it better you can learn ActivityResultContract
Try to use databinding as it helps to reduce many of the boilerplate since you're using kotlin.
Try to adapt to MVVM architecture as it helps you a lot in developing Android app.
I'm trying to make a quiz app so that when the user clicks the plus button it takes them to a page where they input the question and answer which they can then save and is taken back to the previous page where a new button with the text set to the question is created. I have all of this coded and working except for the part where I can save the previously inputted question/answer activity state if the user wants to edit it. I've been told I need to send my question and answer from my main activity to my second activity in the form of a bundle but I have no idea how to access another layouts id in my main activity (and also how to use bundles in general). Can someone please help? Here's my code:
Main Activity:
package com.example.quest
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private val questionActivityCode = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<FloatingActionButton>(R.id.btn2).setOnClickListener{
startActivityForResult(Intent(this#MainActivity, SecondActivity::class.java), questionActivityCode)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == questionActivityCode && resultCode == Activity.RESULT_OK) {
createNewButtonWithText(data?.getStringExtra("test") ?: "")
}
}
private fun createNewButtonWithText(text: String)
{
val newbutton = Button(this#MainActivity)
val layout = findViewById<LinearLayout>(R.id.mainlayout)
newbutton.text = text
newbutton.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
newbutton.width=1010
newbutton.height=300
newbutton.gravity = Gravity.CENTER
newbutton.translationX= 65F
newbutton.setTextColor(Color.parseColor("#FFFFFFFF"))
newbutton.setBackgroundColor(Color.parseColor("#250A43"))
layout.addView(newbutton)
val inflator = layoutInflater
val builder = AlertDialog.Builder(this)
val intent = Intent(this#MainActivity, SecondActivity::class.java)
newbutton.setOnClickListener{
val dialogLayout = inflator.inflate(R.layout.text, null)
with(builder) {
setTitle(newbutton.text)
setPositiveButton("Edit"){dialog, which ->
startActivity(intent)
}
setNegativeButton("Cancel"){dialog, which ->
Log.d("Main", "Negative button clicked")
}
setView(dialogLayout)
show()
}
}
}}
Second Activity:
package com.example.quest
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.EditText
import com.google.android.material.floatingactionbutton.FloatingActionButton
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val question = findViewById<EditText>(R.id.question)
findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener{
val questiontext = question.text.toString()
val returnIntent = Intent()
returnIntent.putExtra("test", questiontext)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
}
}
Im not sure what do you want to achive. but I am trying to seperate your question into 2 questions.
You want to save your inputed text, so that you can edit it latter.
You can use Shared Preference Manager
Sample :
/*Save your values to SharedPreference*/
// create shared Prefrence Manager
// edit() This allows you to write key-value pairs to
//SharedPreferences.
val sharedPre = PreferenceManager.getDefaultSharedPreferences(applicationContext).edit()
// add key-value pairs to SharedPreferences
sharedPre.putString("Key","Value")
// Instruct the SharedPreferences Editor instance to apply the changes.
sharedPre.apply()
/*Load your values from SharedPreference*/
val loadSharedPre = PreferenceManager.getDefaultSharedPreferences(applicationContext)
// its return Map with string and value
loadSharedPre.all
send my question and answer from my main activity to my second activity in the form of a bundle
You can use parcelable, for more details you can visi this site
I'm making a quiz app and so far I've made the code for creating a question and answer in one activity which then takes you back to the main activity where a button with the text set to the question is created. I want to have it so that you can click that button and go back into the previous activity where you previously inputted the question and answer specifically for that button and have all of that still there. How exactly would I be able to create something like this? I've only ever seen a Java video for one but never a Kotlin. Here's my code for context:
MainActivity:
package com.example.quest
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private val questionActivityCode = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<FloatingActionButton>(R.id.btn2).setOnClickListener{
startActivityForResult(Intent(this#MainActivity, SecondActivity::class.java), questionActivityCode)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == questionActivityCode && resultCode == Activity.RESULT_OK) {
createNewButtonWithText(data?.getStringExtra("test") ?: "")
}
}
private fun createNewButtonWithText(text: String)
{
val newbutton = Button(this#MainActivity)
val layout = findViewById<LinearLayout>(R.id.mainlayout)
newbutton.text = text
newbutton.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
newbutton.width=1010
newbutton.height=300
newbutton.gravity = Gravity.CENTER
newbutton.translationX= 65F
newbutton.setTextColor(Color.parseColor("#FFFFFFFF"))
newbutton.setBackgroundColor(Color.parseColor("#250A43"))
layout.addView(newbutton)
val inflator = layoutInflater
val dialogLayout = inflator.inflate(R.layout.text, null)
val builder = AlertDialog.Builder(this)
val intent = Intent(this#MainActivity, SecondActivity::class.java)
newbutton.setOnClickListener{
with(builder) {
setTitle(newbutton.text)
setPositiveButton("Edit"){dialog, which ->
startActivity(intent)
}
setNegativeButton("Cancel"){dialog, which ->
Log.d("Main", "Negative button clicked")
}
setView(dialogLayout)
show()
}
}
}}
SecondActivity:
package com.example.quest
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText
import com.google.android.material.floatingactionbutton.FloatingActionButton
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val question = findViewById<EditText>(R.id.question)
findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener{
val questiontext = question.text.toString()
val returnIntent = Intent()
returnIntent.putExtra("test", questiontext)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
}
}
you can use onSaveIntanceState method to save current activity data
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putString("activity", "my activity data");
// etc.
}
and for retrieve data when activity resumed you have to use savedInstanceState
in onRestoreInstanceState method or in onCreate method
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
String myString = savedInstanceState.getBoolean("activity");
}
I'm trying to make a quiz app so that when the user clicks the plus button it takes them to a page where they input the question and answer which they can then save and is taken back to the previous page where a new button with the text set to the question is created. I have all of this coded and working except for the part where I can save the question/answer activity state if the user wants to edit it. I've tried to use onSaveInstanceState and onRestoreSaveInstanceState but it didn't save the activity at all. Can someone see what I'm doing wrong? Here's my code:
Main Activity:
package com.example.quest
import android.app.Activity
import android.app.AlertDialog
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private val questionActivityCode = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<FloatingActionButton>(R.id.btn2).setOnClickListener{
startActivityForResult(Intent(this#MainActivity, SecondActivity::class.java), questionActivityCode)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == questionActivityCode && resultCode == Activity.RESULT_OK) {
createNewButtonWithText(data?.getStringExtra("test") ?: "")
}
}
private fun createNewButtonWithText(text: String)
{
val newbutton = Button(this#MainActivity)
val layout = findViewById<LinearLayout>(R.id.mainlayout)
newbutton.text = text
newbutton.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
newbutton.width=1010
newbutton.height=300
newbutton.gravity = Gravity.CENTER
newbutton.translationX= 65F
newbutton.setTextColor(Color.parseColor("#FFFFFFFF"))
newbutton.setBackgroundColor(Color.parseColor("#250A43"))
layout.addView(newbutton)
val inflator = layoutInflater
val builder = AlertDialog.Builder(this)
val intent = Intent(this#MainActivity, SecondActivity::class.java)
newbutton.setOnClickListener{
val dialogLayout = inflator.inflate(R.layout.text, null)
with(builder) {
setTitle(newbutton.text)
setPositiveButton("Edit"){dialog, which ->
startActivity(intent)
}
setNegativeButton("Cancel"){dialog, which ->
Log.d("Main", "Negative button clicked")
}
setView(dialogLayout)
show()
}
}
}}
Second Activity:
package com.example.quest
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.EditText
import com.google.android.material.floatingactionbutton.FloatingActionButton
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
val question = findViewById<EditText>(R.id.question)
findViewById<FloatingActionButton>(R.id.btn3).setOnClickListener{
val questiontext = question.text.toString()
val returnIntent = Intent()
returnIntent.putExtra("test", questiontext)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
}
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
Log.i("Instance State", "onSaveInstanceState")
val question = findViewById<EditText>(R.id.question)
val answer = findViewById<EditText>(R.id.answer)
outState.putCharSequence("savedQuestion", question.toString())
outState.putCharSequence("savedAnswer", answer.toString())
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
Log.i("Restored Instance State", "onRestoreInstanceState")
val question = savedInstanceState.getCharSequence("savedQuestion")
val answer = savedInstanceState.getCharSequence("savedAnswer")
val txt = findViewById<EditText>(R.id.question)
val txt2 = findViewById<EditText>(R.id.answer)
txt.setText(question)
txt2.setText(answer)
}
}
That is not the use case for onSaveInstanceState and onRestoreInstanceState. You should use them when your Activity is recreated. For example: device is rotated.
In your case, you are starting a new instance of SecondActivity when you are starting it from MainActivity.
To achieve what you want, you should pass the question and answer from MainActivity to SecondActivity using a Bundle and then let the user edit them.