AsnycTask Not Working - android

EDIT
Okay really simplified version, let's call this my Activity2
public class test {
String username, id;
TextView view1, view2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view1 = (TextView)findViewById(R.id.textView1);
view2 = (TextView)findViewById(R.id.textView2);
new testFunction().execute();
view1.setText(username);
view2.setText(id);
}
private class testFunction extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... x) {
username = "1";
id = "2";
return null;
}
}
}
Activity1 has a submit button that starts Activity2 (this activity). After Activity2 calls onCreate(), "12" will appear on the screen. When I click back (destroy Activity2 and go back to Activity1), AND THEN go back to Activity2 again, nothing appears on the screen. Why is this?

This is one problem
new testFunction().execute("1", "2").get();
You should not use get(). It blocks the ui thread waiting for the result. And you should never block the ui thread. Use
new testFunction().execute("1", "2");
Declare the variables as instance variables
TextView view1,view2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data);
view1 = (TextView)findViewById(R.id.textView1);
view2 = (TextView)findViewById(R.id.textView2);
new testFunction().execute("1", "2")
Then in Asynctask onPostExecute update the views
#override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
// update view here
}

finish the actvity when you have pressed back button like
Intent i = new Intent(this,Here is your class name.Class);
startActivity(i);
finish();
by this whenever you will go in second act by press submit button that time on create call
and you can also call your asnyc task in on resume method every time on resume will call.

You have to store the username and id value when closing the fragment.
Have a look into Fragment lifecycle onSaveInstanceState.
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(ARG_NAME, username);
outState.putBoolean(ARG_ID, id);

I have add some code lines to your onCreate method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data);
//get the values from previous avtivity
Bundle bundle=getIntent().getExtras();
username=bundle.getString("username").toString();
id=bundle.getString("id").toString();
String result = new testFunction().execute("1", "2").get();
TextView view1 = (TextView)findViewById(R.id.textView1);
TextView view2 = (TextView)findViewById(R.id.textView2);
view1.setText(username);
view2.setText(id);
}
in order to get the values you must pass the values from previous activity, I guess you have did it correctly, what you missed is, you forgot to get the Intent values and assign them to username,id variables

Related

Keeping inputs in activity after moving to another activity

I am using Android Studio. I have two activities, MainActivity and Main2Activity. There is an edit text and a button in each one. How do I keep the input in the edit text in any activity when I go to the second activity? I tried many answers but nothing worked.
Here is the code of the activity.
public class MainActivity extends AppCompatActivity {
EditText editText2;
Button button;
String var1 ;
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("var1", var1);
editText2 = (EditText)findViewById(R.id.editText2);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
var1 = savedInstanceState.getString("var1");
var1 = editText2.getText().toString();
editText2.setText(var1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Click1(View view)
{
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
}
To carry over values from one activity to another, you need to add a line of code before startActivity(i);. This line of code that you need to add is:
i.putExtra("inputValue", String.valueOf(editText2.getText()));
Then in the other activity, in the onCreate() method, add this:
Intent intent = getIntent();
String inputValue = intent.getStringExtra("inputValue");
editText.setText(inputValue);
Obviously I don't know the name of your EditText in the MainActivity2 so replace the editText with its name.
Hope this helps!
You need to check for the existence of savedInstanceState in your onCreate function.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check for a savedInstanceState
if(savedInstanceState != null){
var1 = savedInstanceState.getString("var1");
editText2.setText(var1); // Making sure that you have assigned editText2 already, of course.
}
}

Android Intents to pass data

I don't know why, but this isn't working. Can anyone see anything wrong with my code.
I have two activities and I'm passing data from two
public class InputActivity extends AppCompatActivity {
EditText number1EditText;
EditText number2EditText;
Button addButton;
InputActivity code looks like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
number1EditText = (EditText)findViewById(R.id.editText);
number2EditText = (EditText)findViewById(R.id.editText2);
addButton = (Button)findViewById(R.id.button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(getApplicationContext(), AddActivity.class);
mIntent.putExtra("number1", number1EditText.getText().toString());
mIntent.putExtra("number2", number2EditText.getText().toString());
startActivity(mIntent);
}
});
}
AddActivity code looks like this
public class AddActivity extends AppCompatActivity {
TextView answer;
double y=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
answer =(TextView)findViewById(R.id.textView);
String value1 = getIntent().getExtras().getString("number1");
String value2 = getIntent().getExtras().getString("number2");
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
}
Just looking quickly:
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
You're passing a int to the method setText() which happens to have an overload that receives a int argument for the cases when you pass the reference from a String in some xml. You may be getting a ResourceNotFoundException.
If you want to show a text with the sum between your values:
answer.setText(String.valueOf((int) (Double.parseDouble(value1)+Double.parseDouble(value2))));
Just keep in mind that there are a lot of checks you have to do first, you may get another exceptions doing this kind of parse, like NumberFormatException for instance.

Set background of Activity from another one

I'm trying to set a background from ActivityB to AcitivtyA using getInstance but it only show me the Toast message, whenever turn back to ActivityA, there's no change.
I have this in my ActivityA:
private static MainActivity activityA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityA = this;
}
public static MainActivity getInstance(){
return activityA;
}
public void setLeavesBackground() {
FrameLayout mainFrameLyt = (FrameLayout) findViewById(R.id.mainFrameLayout);
mainFrameLyt.setBackgroundColor(Color.parseColor("#00FF00"));
Toast.makeText(getBaseContext(), "New style applied", Toast.LENGTH_SHORT).show();
}
And this in my ActivityB:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_style);
ImageButton leavesBtn = (ImageButton) findViewById(R.id.leavesBtn);
leavesBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.getInstance().setLeavesBackground(); //call myFunction using activityA
}
});
}
What should I do?
You approach to get an Instance of another Activity and modify it from within another activity is wrong:
public static MainActivity getInstance(){
return activityA;
}
You have to obey the Activity lifecycle
A way how you could set another activities background is by creating a shared preferences value for the background color and use it in the onCreate method of your Activity.ñ

How can I activity close in Android?

I have three classActivity created. One is super class and other sub class and third is HomeActivity.
Code for super class is :
public class MyActivity extends Activity {
Button btnHome = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onHomeClick(View view) {
String LOG_TAG = "Akshar";
System.out.println("Hello11111");
btnHome = (Button) view;
Log.v(LOG_TAG, "index=" + btnHome);
}
}
and my subclass code is :
public class ChooseIsland extends MyActivity {
Button btn_home = null;
MyActivity ob1 = new MyActivity();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chooseiland);
addListenerOnButton();
}
private void addListenerOnButton() {
// TODO Auto-generated method stub
ob1.onHomeClick(btn_home);
}
}
Now I want to go on Home page when click so when I write ?
Intent intent = new Intent(this, HomeActivity.class);
There is no close operation as such in android. You should make sure you do not save anything in stack so whenever you are traversing from one activity to other, make sure you use intent flags to clear history or top of stack and then call finish.
finish() will close the current activity and the previous activity will come to foreground.
Generally to we write finish() method to close activity.

How to call function of one activity from another activity

Actually i want to call a function of one activity from another activity..i write a simple code for that but its not working..and show runtime error..please check the code...if there any mistake..
code for activity1:
public class Activity1 extends Activity2
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
call();
}
public void call()
{
showToast("Helloo");
}
}
code for activity2:
public class Activity2 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showToast(String s)
{
EditText t=(EditText)findViewById(R.id.editText1);
t.setText(s);
}
}
Your problem is that you're calling findViewById on a view that doesn't exist.
Activity1 is extending Activity2.
You call the super.onCreate in Activity1 which calls the onCreate in Activity2 which calls setContentView() for R.layout.main.
I'm guessing your text R.id.editText1 is in the main layout.
When Activity1 returns from the call to super.onCreate it immediately resets the content layout to main2.
The edit text box that you are trying to edit no longer exists. findViewById can not find it because the layout is not active. Thus it crashes.
To fix it try this:
public class Activity1 extends Activity2
{
private EditText et;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
et = (EditText) findViewById(R.id.editText2);
call();
}
public void call()
{
showToast("Helloo", et);
}
}
Where R.id.editText2 is an edit text box in the layout main2.
In Activity2:
public class Activity2 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showToast(String s, EditText t)
{
t.setText(s);
}
}
First, this is a bad design principle since only one Activity is active at a time. You can make a method static and then you can cross call them but at that point it should be in some sort of common util class.
The simplest way is to declare your showToast() method as public static, this way you can call it without having an instance of Activity2.
if you put it in as static you should declare it on your main activity

Categories

Resources