How can i pass variable from OnCreate to function(View view)

Related searches

i have an activity that get a variable from another activity, and i want to display this variable when user click on button. the problem is SaveFile(View view) method cannot find "SurveyTilte" variable. How can i pass this variable?

public class CreateSurvey extends AppCompatActivity {
    TextView Textfile;
    String SurveyTilte;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_survey);
        Bundle extras = getIntent().getExtras();
        SurveyTilte = extras.getString("SurveyTilte");
}
}

i can't, but the code in OnClickListener because there is an @Override method

public void SaveFile(View view) {
Textfile = (TextView) findViewById(R.id.surveyDetails);
Textfile.setText(SurveyTilte);
}
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case PERMISSION_REQUEST:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(CreateSurvey.this, "Permission granted", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(CreateSurvey.this, "Permission not granted", Toast.LENGTH_SHORT).show();
                    finish();
                }
        }

This is actually more a matter of java than android.

By doing:

protected void onCreate(Bundle savedInstanceState) {
    ...
    String SurveyTilte = extras.getString("SurveyTilte");
}

What you actually do is creating a local variable separated from your class field variable with the same name, and is 'alive' and available for that specific method scope. Meaning you'll loose its reference (and thus its value) when exiting onCreate method.

What you need to do:

All you need to do is not to declare a new variable inside onCreate but to use the same variable you declared in advanced in your class attributes.

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    SurveyTilte = extras.getString("SurveyTilte");
}

That's how you can use the same reference for your value in the entire class scope.

Passing parameters from Activity.onCreate into other methods , setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub String keyText = keyT.getText(). I'm trying to create a function that will use an EditText variable that is not a parameter of the function (or it's called method, please correct me). I know that with java I use static for the variable and declare the variable from outside a class, but it gives me all kind of errors, how can I use this variable in the function?

Remove String keyword from String SurveyTilte = extras.getString("SurveyTilte"); to assign value to the class field and not local variable:

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_survey);
        Bundle extras = getIntent().getExtras();
        SurveyTilte = extras.getString("SurveyTilte");
}

and then

public void SaveFile(View view) {
  Textfile = (TextView) findViewById(R.id.surveyDetails);
  Textfile.setText(SurveyTilte); // <-- reference field that contains value from intent data
}

How to reach a variable from outside the function in Android , I'm trying to create a function that will use an EditText variable that is not a protected void onCreate(Bundle savedInstanceState) { // Creating the random MainActivity$1.onClick(MainActivity.java:58) at android.view.View. I am passing the value using viewbag variable into view and then that value should be passed as paramter in to my ajax function call My controller public ActionResult LeaversOverviewReport(DateTime

I think this can help you.

public class CreateSurvey extends AppCompatActivity {
 TextView Textfile;
 String SurveyTilte;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.create_survey);
     }
  }



public void SaveFile(View view) {
  Bundle extras = getIntent().getExtras();
  if(extras != null) {
    SurveyTilte = extras.getString("SurveyTilte");
    Textfile = (TextView) findViewById(R.id.surveyDetails);
    Textfile.setText(SurveyTilte);
  } 
}

Passing data between Activities using Intent in Android, First of all, we have to link the views of activity_login.xml with LoginActivity In protected void onCreate(Bundle savedInstanceState) { To pass the data through Intent we will use putExtra() method and in parameter, we will� Example of onCreate(Bundle savedInstanceState): Lets create a simple program to understand onCreate(Bundle savedInstanceState) more deeply. In the below example we will ask the user to Enter the name, save his name in Bundle object and then display the same name when user rotates the screen.

Activity, params, ViewGroup.LayoutParams : Layout parameters for the view. String, int) method by passing in this activity's class name as the preferences name. ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary. Let's see how it is used. Pass Data Between Controller and View. In the above image, you can see how data flows from the "Controller" to the "View", and how it looks in the browser. Pass Data Between View to View

Pass data between fragments, Google is committed to advancing racial equity for Black communities. See how. Android Developers � Docs � Guides� All the tutorials I can find also display a one-page view. However, for some reason, my default view after clicking the Reading View button is a two-page view. I want to switch back to one-page view, yet I could not find any options to switch between one-page and two-page view. Please help. Thank you.

I am trying to pass some variables from an activity to a fragment and then back to an activity. Inside the onCreate of the fragment, getArguments() is not null and I am able to get the variables. But, inside the onClick of the fragment (which triggers going back to the activity), getArguments() and the variables I retrieved in the onCreate are

Comments
  • but it give me a null variable when Textfile.setText(SurveyTilte);
  • there is no error ,but when "SurveyTilte " get its value from OnCreate and call it from SaveFile(View view) method its value is null, i removed String SurveyTilte from OnCreate but it still null
  • And did you check that in onCreate it actually gets a non null value?
  • yes, when i try Textfile.setText(SurveyTilte); in onCreate It Working 100%
  • Do you change SurveyTilte's value anywhere else in CreateSurvey activity? Does this SaveFile method is in CreateSurvey activity?
  • yes but it still give my null Text, i mean when "SurveyTilte " get its value from OnCreate and call it from SaveFile(View view) method its value is null, i removed String SurveyTilte from OnCreate but it still null
  • What value saved in SurveyTilte immediately after onCreate? Does intent's extra actually contain data?
  • yes, when i try Textfile.setText(SurveyTilte); in onCreate It Working
  • Are there any other places you reassign SurveyTilte?
  • Finally, could you share entire CreateSurvey activity code?