Display String in TextView after user input in another classHow to display HTML in TextView?Android simple login screen crashes app when logging, no errors in eclipse. Which java class activity is at fault?bufferedReader for URL throws IOExceptionGetting an exception when app startsForce close onClick of ImageButton, can't figure out whyjava.lang.NullPointerException in textview inside asyncTasksetText on button from another activity androidAdding Social Media Share Logic From Firebase in AndroidActivity can still call unbind service method. Is it normal?java.lang.NullPointerException when invoking onLoadFinished()

Can you pop microwave popcorn on a stove?

The pirate treasure of Leatherback Atoll

Yet another calculator problem

UK citizen travelling to France at the end of November

How there are 3 possible tautomers of 2,2,4-trimethylheptane-3,5-dione?

Is it unavoidable taking shortcuts in software development sometimes?

What makes things real?

What can we do about our 9-month-old putting fingers down his throat?

Friend is very nitpicky about side comments I don't intend to be taken too seriously

How is lower/no gravity simulated on a planet with gravity, without leaving the surface?

How to descend a few exposed scrambling moves with minimal equipment?

What is the difference between tl_to_str:V and tl_to_str:N?

Bacteria vats to generate edible biomass, require intermediary species?

Quick Shikaku Puzzle: Stars and Stripes

The meaning of "offing" in "an agreement in the offing"

Is there a specific way to describe over-grown, old, tough vegetables?

Does the word voltage exist in academic engineering?

What's the biggest difference between these two photos?

Contractor cut joist hangers to make them fit

Isn't that (two voices leaping to C like this) a breaking of the rules of four-part harmony?

How do you say "to hell with everything" in French?

Does the 2019 UA artificer need to prepare the Lesser Restoration spell to cast it with their Alchemical Mastery feature?

Capacitors with same voltage, same capacitance, same temp, different diameter?

Why can linguists decide which use of language is correct and which is not?



Display String in TextView after user input in another class


How to display HTML in TextView?Android simple login screen crashes app when logging, no errors in eclipse. Which java class activity is at fault?bufferedReader for URL throws IOExceptionGetting an exception when app startsForce close onClick of ImageButton, can't figure out whyjava.lang.NullPointerException in textview inside asyncTasksetText on button from another activity androidAdding Social Media Share Logic From Firebase in AndroidActivity can still call unbind service method. Is it normal?java.lang.NullPointerException when invoking onLoadFinished()






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-3















I am trying to pass a String from a class (UserInput) to Main Activity and display it in a TextView. User is supposed to press a button in Main Activity, which calls UserInput and enter a String. Using Shared Preferences, I managed to pass the String over, but could not get it to display in the TextView.



I realized that the getSharedPreference and setText part was done before the class was started, resulting in the TextView not being updated after returning from UserInput and I have no idea how to solve this issue. Any help will be much appreciated. Thanks!



Main Activity code



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

TextView tvCode;
Button btnCode, btnClear;
TextInputLayout textInputMessage;
String preferenceFileName, preferenceKey, retreivedCodeString;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvCode = findViewById(R.id.textViewCode);
btnCode = findViewById(R.id.buttonCode);
btnClear = findViewById(R.id.buttonClear);
textInputMessage = findViewById(R.id.textInputLayoutMessage);
preferenceFileName = "PreferenceFile";
preferenceKey = "CodeKey";


SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);

btnCode.setOnClickListener(this);
btnClear.setOnClickListener(this);



@Override
public void onClick(View arg0)
switch (arg0.getId())
case R.id.buttonClear:
//method
break;
case R.id.buttonCode:
Intent intent = new Intent(this, UserInput.class);
startActivity(intent);
sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);
break;





UserInput code



public class UserInput extends Activity implements View.OnClickListener {
String userInputCode, preferenceFileName, preferenceKey;
TextInputLayout subTextInputLayoutCode;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.code);

subTextInputLayoutCode = findViewById(R.id.textInputLayoutCode);
Button btnOk = findViewById(R.id.buttonOk);
Button btnCancel = findViewById(R.id.buttonCancel);
preferenceFileName = "PreferenceFile";
preferenceKey = "CodeKey";

btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);


@Override
public void onClick(View arg0)
switch (arg0.getId())
case R.id.buttonOk:
if (!validateCode())
return;

userInputCode = subTextInputLayoutCode.getEditText().getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(preferenceKey,userInputCode);
editor.apply();
finish();
break;
case R.id.buttonCancel:
finish();
break;











share|improve this question
























  • Someone will definitely post the solutions, but I would seriously suggest to move to Fragments for the use case you are trying to address.

    – Mohammed Atif
    Mar 28 at 8:18


















-3















I am trying to pass a String from a class (UserInput) to Main Activity and display it in a TextView. User is supposed to press a button in Main Activity, which calls UserInput and enter a String. Using Shared Preferences, I managed to pass the String over, but could not get it to display in the TextView.



I realized that the getSharedPreference and setText part was done before the class was started, resulting in the TextView not being updated after returning from UserInput and I have no idea how to solve this issue. Any help will be much appreciated. Thanks!



Main Activity code



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

TextView tvCode;
Button btnCode, btnClear;
TextInputLayout textInputMessage;
String preferenceFileName, preferenceKey, retreivedCodeString;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvCode = findViewById(R.id.textViewCode);
btnCode = findViewById(R.id.buttonCode);
btnClear = findViewById(R.id.buttonClear);
textInputMessage = findViewById(R.id.textInputLayoutMessage);
preferenceFileName = "PreferenceFile";
preferenceKey = "CodeKey";


SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);

btnCode.setOnClickListener(this);
btnClear.setOnClickListener(this);



@Override
public void onClick(View arg0)
switch (arg0.getId())
case R.id.buttonClear:
//method
break;
case R.id.buttonCode:
Intent intent = new Intent(this, UserInput.class);
startActivity(intent);
sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);
break;





UserInput code



public class UserInput extends Activity implements View.OnClickListener {
String userInputCode, preferenceFileName, preferenceKey;
TextInputLayout subTextInputLayoutCode;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.code);

subTextInputLayoutCode = findViewById(R.id.textInputLayoutCode);
Button btnOk = findViewById(R.id.buttonOk);
Button btnCancel = findViewById(R.id.buttonCancel);
preferenceFileName = "PreferenceFile";
preferenceKey = "CodeKey";

btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);


@Override
public void onClick(View arg0)
switch (arg0.getId())
case R.id.buttonOk:
if (!validateCode())
return;

userInputCode = subTextInputLayoutCode.getEditText().getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(preferenceKey,userInputCode);
editor.apply();
finish();
break;
case R.id.buttonCancel:
finish();
break;











share|improve this question
























  • Someone will definitely post the solutions, but I would seriously suggest to move to Fragments for the use case you are trying to address.

    – Mohammed Atif
    Mar 28 at 8:18














-3












-3








-3








I am trying to pass a String from a class (UserInput) to Main Activity and display it in a TextView. User is supposed to press a button in Main Activity, which calls UserInput and enter a String. Using Shared Preferences, I managed to pass the String over, but could not get it to display in the TextView.



I realized that the getSharedPreference and setText part was done before the class was started, resulting in the TextView not being updated after returning from UserInput and I have no idea how to solve this issue. Any help will be much appreciated. Thanks!



Main Activity code



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

TextView tvCode;
Button btnCode, btnClear;
TextInputLayout textInputMessage;
String preferenceFileName, preferenceKey, retreivedCodeString;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvCode = findViewById(R.id.textViewCode);
btnCode = findViewById(R.id.buttonCode);
btnClear = findViewById(R.id.buttonClear);
textInputMessage = findViewById(R.id.textInputLayoutMessage);
preferenceFileName = "PreferenceFile";
preferenceKey = "CodeKey";


SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);

btnCode.setOnClickListener(this);
btnClear.setOnClickListener(this);



@Override
public void onClick(View arg0)
switch (arg0.getId())
case R.id.buttonClear:
//method
break;
case R.id.buttonCode:
Intent intent = new Intent(this, UserInput.class);
startActivity(intent);
sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);
break;





UserInput code



public class UserInput extends Activity implements View.OnClickListener {
String userInputCode, preferenceFileName, preferenceKey;
TextInputLayout subTextInputLayoutCode;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.code);

subTextInputLayoutCode = findViewById(R.id.textInputLayoutCode);
Button btnOk = findViewById(R.id.buttonOk);
Button btnCancel = findViewById(R.id.buttonCancel);
preferenceFileName = "PreferenceFile";
preferenceKey = "CodeKey";

btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);


@Override
public void onClick(View arg0)
switch (arg0.getId())
case R.id.buttonOk:
if (!validateCode())
return;

userInputCode = subTextInputLayoutCode.getEditText().getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(preferenceKey,userInputCode);
editor.apply();
finish();
break;
case R.id.buttonCancel:
finish();
break;











share|improve this question














I am trying to pass a String from a class (UserInput) to Main Activity and display it in a TextView. User is supposed to press a button in Main Activity, which calls UserInput and enter a String. Using Shared Preferences, I managed to pass the String over, but could not get it to display in the TextView.



I realized that the getSharedPreference and setText part was done before the class was started, resulting in the TextView not being updated after returning from UserInput and I have no idea how to solve this issue. Any help will be much appreciated. Thanks!



Main Activity code



public class MainActivity extends AppCompatActivity implements View.OnClickListener {

TextView tvCode;
Button btnCode, btnClear;
TextInputLayout textInputMessage;
String preferenceFileName, preferenceKey, retreivedCodeString;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvCode = findViewById(R.id.textViewCode);
btnCode = findViewById(R.id.buttonCode);
btnClear = findViewById(R.id.buttonClear);
textInputMessage = findViewById(R.id.textInputLayoutMessage);
preferenceFileName = "PreferenceFile";
preferenceKey = "CodeKey";


SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);

btnCode.setOnClickListener(this);
btnClear.setOnClickListener(this);



@Override
public void onClick(View arg0)
switch (arg0.getId())
case R.id.buttonClear:
//method
break;
case R.id.buttonCode:
Intent intent = new Intent(this, UserInput.class);
startActivity(intent);
sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);
break;





UserInput code



public class UserInput extends Activity implements View.OnClickListener {
String userInputCode, preferenceFileName, preferenceKey;
TextInputLayout subTextInputLayoutCode;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.code);

subTextInputLayoutCode = findViewById(R.id.textInputLayoutCode);
Button btnOk = findViewById(R.id.buttonOk);
Button btnCancel = findViewById(R.id.buttonCancel);
preferenceFileName = "PreferenceFile";
preferenceKey = "CodeKey";

btnOk.setOnClickListener(this);
btnCancel.setOnClickListener(this);


@Override
public void onClick(View arg0)
switch (arg0.getId())
case R.id.buttonOk:
if (!validateCode())
return;

userInputCode = subTextInputLayoutCode.getEditText().getText().toString();
SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(preferenceKey,userInputCode);
editor.apply();
finish();
break;
case R.id.buttonCancel:
finish();
break;








android textview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 28 at 7:36









Tan YiLiangTan YiLiang

172 bronze badges




172 bronze badges















  • Someone will definitely post the solutions, but I would seriously suggest to move to Fragments for the use case you are trying to address.

    – Mohammed Atif
    Mar 28 at 8:18


















  • Someone will definitely post the solutions, but I would seriously suggest to move to Fragments for the use case you are trying to address.

    – Mohammed Atif
    Mar 28 at 8:18

















Someone will definitely post the solutions, but I would seriously suggest to move to Fragments for the use case you are trying to address.

– Mohammed Atif
Mar 28 at 8:18






Someone will definitely post the solutions, but I would seriously suggest to move to Fragments for the use case you are trying to address.

– Mohammed Atif
Mar 28 at 8:18













1 Answer
1






active

oldest

votes


















2
















Way 1:



use below code in onResume method of your MainActivity



SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);


Way 2:



start your UserInput activity for result (startActivitForResult(intent, 123))



and finish your UserInput activity using below code



setResult(Activity.RESULT_OK)
finish()


in your main activity override onActivityResultMethode like below



override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
if(requestCode == 123 && resultCode == Activity.RESULT_OK)
SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
tvCode.setText(retreivedCodeString);
else
super.onActivityResult(requestCode, resultCode, data)




Way 3: (without sharedPreferences )



start your UserInput activity for result (startActivitForResult(intent, 123))



and finish your UserInput activity using below code



Intent i = new Intent();
Bundle b = new Bundle();
b.putString("inputText", YOUR_INPUT_TEXT);
i.putExtras(b);
setResult(123, i)
finish()


in your main activity override onActivityResultMethode like below



override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
if(requestCode == 123 && resultCode == Activity.RESULT_OK)
tvCode.setText(data.getExtras().getString("inputText",""));
else
super.onActivityResult(requestCode, resultCode, data)







share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55392314%2fdisplay-string-in-textview-after-user-input-in-another-class%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2
















    Way 1:



    use below code in onResume method of your MainActivity



    SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
    retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
    tvCode.setText(retreivedCodeString);


    Way 2:



    start your UserInput activity for result (startActivitForResult(intent, 123))



    and finish your UserInput activity using below code



    setResult(Activity.RESULT_OK)
    finish()


    in your main activity override onActivityResultMethode like below



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
    if(requestCode == 123 && resultCode == Activity.RESULT_OK)
    SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
    retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
    tvCode.setText(retreivedCodeString);
    else
    super.onActivityResult(requestCode, resultCode, data)




    Way 3: (without sharedPreferences )



    start your UserInput activity for result (startActivitForResult(intent, 123))



    and finish your UserInput activity using below code



    Intent i = new Intent();
    Bundle b = new Bundle();
    b.putString("inputText", YOUR_INPUT_TEXT);
    i.putExtras(b);
    setResult(123, i)
    finish()


    in your main activity override onActivityResultMethode like below



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
    if(requestCode == 123 && resultCode == Activity.RESULT_OK)
    tvCode.setText(data.getExtras().getString("inputText",""));
    else
    super.onActivityResult(requestCode, resultCode, data)







    share|improve this answer































      2
















      Way 1:



      use below code in onResume method of your MainActivity



      SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
      retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
      tvCode.setText(retreivedCodeString);


      Way 2:



      start your UserInput activity for result (startActivitForResult(intent, 123))



      and finish your UserInput activity using below code



      setResult(Activity.RESULT_OK)
      finish()


      in your main activity override onActivityResultMethode like below



      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
      if(requestCode == 123 && resultCode == Activity.RESULT_OK)
      SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
      retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
      tvCode.setText(retreivedCodeString);
      else
      super.onActivityResult(requestCode, resultCode, data)




      Way 3: (without sharedPreferences )



      start your UserInput activity for result (startActivitForResult(intent, 123))



      and finish your UserInput activity using below code



      Intent i = new Intent();
      Bundle b = new Bundle();
      b.putString("inputText", YOUR_INPUT_TEXT);
      i.putExtras(b);
      setResult(123, i)
      finish()


      in your main activity override onActivityResultMethode like below



      override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
      if(requestCode == 123 && resultCode == Activity.RESULT_OK)
      tvCode.setText(data.getExtras().getString("inputText",""));
      else
      super.onActivityResult(requestCode, resultCode, data)







      share|improve this answer





























        2














        2










        2









        Way 1:



        use below code in onResume method of your MainActivity



        SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
        retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
        tvCode.setText(retreivedCodeString);


        Way 2:



        start your UserInput activity for result (startActivitForResult(intent, 123))



        and finish your UserInput activity using below code



        setResult(Activity.RESULT_OK)
        finish()


        in your main activity override onActivityResultMethode like below



        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
        if(requestCode == 123 && resultCode == Activity.RESULT_OK)
        SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
        retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
        tvCode.setText(retreivedCodeString);
        else
        super.onActivityResult(requestCode, resultCode, data)




        Way 3: (without sharedPreferences )



        start your UserInput activity for result (startActivitForResult(intent, 123))



        and finish your UserInput activity using below code



        Intent i = new Intent();
        Bundle b = new Bundle();
        b.putString("inputText", YOUR_INPUT_TEXT);
        i.putExtras(b);
        setResult(123, i)
        finish()


        in your main activity override onActivityResultMethode like below



        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
        if(requestCode == 123 && resultCode == Activity.RESULT_OK)
        tvCode.setText(data.getExtras().getString("inputText",""));
        else
        super.onActivityResult(requestCode, resultCode, data)







        share|improve this answer















        Way 1:



        use below code in onResume method of your MainActivity



        SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
        retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
        tvCode.setText(retreivedCodeString);


        Way 2:



        start your UserInput activity for result (startActivitForResult(intent, 123))



        and finish your UserInput activity using below code



        setResult(Activity.RESULT_OK)
        finish()


        in your main activity override onActivityResultMethode like below



        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
        if(requestCode == 123 && resultCode == Activity.RESULT_OK)
        SharedPreferences sharedPreferences = getSharedPreferences(preferenceFileName, MODE_PRIVATE);
        retreivedCodeString = sharedPreferences.getString(preferenceKey,"");
        tvCode.setText(retreivedCodeString);
        else
        super.onActivityResult(requestCode, resultCode, data)




        Way 3: (without sharedPreferences )



        start your UserInput activity for result (startActivitForResult(intent, 123))



        and finish your UserInput activity using below code



        Intent i = new Intent();
        Bundle b = new Bundle();
        b.putString("inputText", YOUR_INPUT_TEXT);
        i.putExtras(b);
        setResult(123, i)
        finish()


        in your main activity override onActivityResultMethode like below



        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
        if(requestCode == 123 && resultCode == Activity.RESULT_OK)
        tvCode.setText(data.getExtras().getString("inputText",""));
        else
        super.onActivityResult(requestCode, resultCode, data)








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 28 at 8:07

























        answered Mar 28 at 7:46









        Muhammad Muzammil SharifMuhammad Muzammil Sharif

        4853 silver badges14 bronze badges




        4853 silver badges14 bronze badges



















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.




















            draft saved

            draft discarded















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55392314%2fdisplay-string-in-textview-after-user-input-in-another-class%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript