Add view outside of onCreate in android - android

How can I add view bye function, outside of onCreate() in Android ?
My MainActivity.java
public class Main extends Activity {
static RelativeLayout mainRel;
static LinearLayout ll;
static TextView title;
static Context context;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout mainRel=(RelativeLayout) findViewById(R.id.mainRel);
}
public static void refresh(){
LinearLayout ll = new LinearLayout(this); // actually "this" just works in onCreate;
TextView title = new TextView(this);// actually "this" just works in onCreate;
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(title);
title.setText("TV");
mainRel.addView(ll);
}
}
}
Thanks in advance

Usually, UI-related operations are not done in static methods. If you remove the static keyword, the this pointer will work even outside onCreate().
If you insist on keeping the static keyword (because you need the method to be static) then, you should pass a Context parameter to the method, by changing it to refresh(Context context)
Edit: if you need to call this method from another class, you might want to create a reference to your Main Activity and pass it to this other class, then call myMainActivity.refresh()

Calling static method is really bad idea.
Instead you can provide reference of the Activity to your AsyncTask in constructor. Then call non-static refresh() method from AsyncTask#onPostExecute().
When you store reference to Activity in AsyncTask use WeakReference. In case your activity is destroyed while background task is working, it won't be held in memory till background ends.
public class YourAsyncTask extends AsyncTask<Void, Void, Void> {
private WeakRefrence<Main> mainRef;
public YourAsyncTask(Main activity) {
mainRef = new WeakReference<Main>(activity);
}
protected void onPostExecute(Void result) {
Main main = mainRef.get();
if (main != null) {
main.refresh();
}
}
}

Related

finish() not finishing the activity: Android

I have an Activity in whose onCreate() method i call a Utility function.
This utility functions requires a callback class instance as a parameter, in which it returns the info that i need. this is:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utility.functionA(new functionACallBack() {
/**
*
*/
private static final long serialVersionUID = -7896922737679366614L;
#Override
public void onResponse(String error) {
((MyActivity) AppClass.getAppContext()).finish();
}
});
}
Once I have obtained that info, I want to close the activity. so i called finish() from inside the anonymous class that i created for the callback.
But the activity is not getting finished. I thought maybe i need to call finish() from UI thread so i did runOnUiThread(), in inside it also i tried calling finish(). But it just doesn't work.
Could someone please help me with this issue?
UPDATE:
I am storing APP context and then trying to use that but to no avail.
public class AppClass extends Application {
private static Context mContext;
#Override
public void onCreate() {
super.onCreate();
AppClass.mContext = getApplicationContext();
}
public static Context getAppContext(){
return AppClass.mContext;
}
}
Simply call something like this:
#Override
public void onResponse(String error) {
((Activity) context).finish();
}
As this is a static function, you'll have to be able to access your Context in a static way. You can save that as a Class variable, but you'll have to be aware about its handling as it might lead to memory leaks.
To avoid them, you can declare a class that extends Application and save here your context, so this way you won't ever have a memory leak.
Try using this code:
((Activity) ActivityClass.this).finish();
Remember, use the Activity class, not the Application one.

Android app crashes when defining and setting element properties

My app is crashing. What am I doing wrong?
I am using an AsyncTask in a class fetchsSchools.
public class fetchSchools extends AsyncTask<Void, Void, ArrayList<String>>{
#Override
protected ArrayList<String> doInBackground(Void... arg0) {
ArrayList<School> schools = new ArrayList<School>();
ArrayList<String> schoolNames = new ArrayList<String>();
... code omitted for conciseness...
return schoolNames;
}
In this class I have an onPost Execute, my code gets to this, if I remark out the cls2 lines my app runs:
public void onPostExecute(ArrayList<String> schoolNames) {
MainActivity cls2=new MainActivity();
cls2.updateSpinner(schoolNames);
cls2.switchScreens();
}
The above fires off these two outines back in MainActivity which crash the app:
public void updateSpinner(ArrayList<String> schoolNames) {
Spinner schoolSpinner = (Spinner)findViewById(R.id.school_spinner);
schoolSpinner.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, schoolNames));
}
public void switchScreens() {
ProgressBar progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
progressBar1.setVisibility(View.GONE);
TextView loading_label = (TextView)findViewById(R.id.loading_label);
loading_label.setVisibility(View.GONE);
}
Eclipse isn't showing any coding errors. Am I creating and acting on these variables correctly?
Though you have not given code for MainActivity and logcat. Seeing your code looks like MainActivity extends Activity. If so you cannot call
MainActivity cls2=new MainActivity();
MainActivity has to be initialized by android framework with appropriate context. Calling constructor yourself will not call any lifecycle methods of the Activity.
So all subsequent calls that use Context will fail, especially findViewByid
You need to do startActivity instead of what you are doing.
Edit:
Create a constructor in Asynctask pass MainActivity when instantiating and assign like below and remove new MainActivity line
public class fetchSchools extends AsyncTask<Void, Void, ArrayList<String>>{
MainActivity cls2;
fetchSchools(MainActivity activity){
cls2 = activity;
}
#Override
protected ArrayList<String> doInBackground(Void... arg0) {
ArrayList<School> schools = new ArrayList<School>();
ArrayList<String> schoolNames = new ArrayList<String>();
... code omitted for conciseness...
return schoolNames;
}
public void onPostExecute(ArrayList<String> schoolNames) {
cls2.updateSpinner(schoolNames);
cls2.switchScreens();
}
}
In MainActivity, you call
new fetchSchools(mMainActivity).execute(param);
If this is a seperate class from that your main activity, then you're trying to findViewById from where? You probably get NullPointerException on very first line of onPostExecute which is this one:
cls2.updateSpinner(schoolNames);
Because you try to find a view in this method. But you do not have any parent view which contains the view that you seek to find.
So i would suggest you to move this fetchSchools class as a private inner class of your mainActivity. so you'll be able to find those views globally and set them however you want.

pass UI Controls from activity to a class

I stuck at this issue many times and I passed the problem in different ways and I'm not sure that I made it in the right way.
I simplified the problem in a the following example. I know that I can pass only the data to the class but I do want to pass the editText cause I have this problem with more difficult UI controls.
mainactivity.java
public class mainactivity extends Activity {
public EditText clickEditText;
int count =0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
newTxt();
}
public void newTxt() {
txt = new MyText(context);
txt.updateTextEdit("Main Activity");
}
}
myText.java
public class MyText
{
private Context _context;
// constructor
public MyText(Context context)
{
_context = context;
}
public void updateTextEdit(String str)
{
private EditText strEditText;
strEditText= (EditText)findViewById(_context.R.id.editTextClick); // ????
strEditText.setText(str + " and myTxt");
}
}
if you could explain me how to fix the updateTextEdit function. i passed the context of the main activity. How can I change the editText? Thank you very much!!!
If you really want to do this this way, you need to save a reference to Activity, not Context. Like this:
public class MyText
{
private Activity _activity;
// constructor
public MyText(Activity activity)
{
_activity= activity;
}
public void updateTextEdit(String str)
{
private EditText strEditText;
strEditText= (EditText)activity.findViewById(R.id.editTextClick);
strEditText.setText(str + " and myTxt");
}
}
and in newTxt() you will need to change:
txt = new MyText(context);
to:
txt = new MyText(this);
But wouldn't it be easier to just put this method inside your activity? Why do you want it in another class? If it really needs to be in another class, you could make that class an inner class of your activity and you would still have access to the activity's methods and member variables.
There's a similar question here
How to access Activity UI from my class?
You didn't say how you obtained the context, you should use this and get the mainactivity in the other class. not context.
then you can call runOnUIThread to perform UI updates.

Finishing an activity from a standard java class

I am currently working on an android project and I have an activity, lets call it MyActivity and this activity calls a standard Java class called MyClass.
I need MyClass to finish the MyActivity activity but I can't find out how to do this. I thought I might be able to pass the context to the standard java class and call context.finish() but this doesn't appear to be available.
How can I do this, thanks for any help you can offer.
You can pass the Context, but you will need to cast it to an Activity (or simply pass the Activity itself), although this in general seems like a bad practice.
The most secure solution uses listener and a Handler. It is complex, but ensures a non direct call to finish activity.
Your listener:
interface OnWantToCloseListener{
public void onWantToClose();
}
Class that should close activity.
class MyClass {
private OnWantToCloseListener listener;
public void setWantToCloseListener(OnWantToCloseListener listener){
this.listener = listener;
}
private void fireOnWantToClose(){
if(this.listener != null)
listener.onWantToClose();
}
}
When you want to close your activity you must call fireOnWantToClose() method.
public MyActivity extends Activity{
public void onCreate(){
final int CLOSE = 1; //number to identify what happens
MyClass my_class = new MyClass();
final Handler handler = new Handler(){
public void handleMessage(Message msg){
if(msg.what == CLOSE)
MyActivity.this.finish();
}
});
my_class.setOnWantToCloseListener(new OnWantToCloseListener(){
public void onWantToClose(){
handler.sendEmptyMessage(CLOSE);
}
});
}
}
This is secure because Activity is not finished directly by MyClass object, it is finished through a listener that orders a handler to finish activity. Even if you run MyClass object on a second thread this code will works nice.
EDIT: CLOSE var added I forget to declare and initialize this.
Pass the MyActivity to MyClass as an Activity. From there you can call myActivity.finish();
For example:
private Activity myActivity;
public MyClass(Activity myActivity){
this.myActivity = myActivity;
}
public void stopMyActivity(){
myActivity.finish();
}
And in MyActivity:
MyClass myClass = new MyClass(this);
This is risky, because you're holding a reference to an Activity, which can cause memory leaks.
If your java class is a nested inner class, you can use:
public class MyActivity extends Activity {
public static class JavaClass {
public void finishActivity() {
MyActivity.finish();
}
}
}
Otherwise you'll have to pass the java class a Context (i.e. pass it a reference to this, since Activity extends Context) and store it as a private instance variable.

Is it possible to access an object of an activity fom other place?

This maybe a stupid idea, but does anyone know is it possible to access one activity's object form other places?
To be specific, lets say if you have an activity A (with a textView t) and you create a normal java class B.
At onCreate, you start to run B for some calculation like below,
public class MyActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = (TextView)findViewById(R.id.outputtext);
Somejava B = new Somejava();
B.run();
}
}
Is there a way for B to update the textView?
I know the simple way (maybe the correct way) is to return the result from B class and use t.setText(result) in MyActivity, but I'm just want to know is it possible to update the textview in B?
Use Intent or public static variables
can simply pass activity refernce to b in constructor and create the method in your acitivty to update textview. if you using another thread not forgot to use handler or other ways to update UI thread.
Yes, it is possible if the Activity's field is public and post the UI changes in a public Handler created on the first Activity but in facts, it's really ugly to do that...
You can use startActivityForResult(...) to notify an other activity how the process has passed with some serialiezable data in the Bundle extras of the Intent and catch the result in the overrided method onActivityResult(...)...
For a "normal java class" B I would work with interfaces
public interface SomejavaListener{
void onSomejavaFinish(Object result);
}
public class MyActivity implements SomejaveFinish extends Activity {
TextView t;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = (TextView)findViewById(R.id.outputtext);
Somejava B = new Somejava();
B.run(MyActivity.this); //notice the extra argument!
}
public void onSomejavaFinish(Object result){
t.setText("updated! ^,^");
}
}
public class Somejava {
//...
public void run(SomejavaListener callback){
//working working
callback.onSomejavaFinish( new Object() );
}
}
However in respect to the android environment the question is sitting in I got the feeling maybe an AsyncTask would be the right thing for you. It has an doInBackground method to do work and not spoiling your UI Thread (resulting in ANR Errors.)
Another advantage is the onPreExecute and onPostExecute methods are running in the UI Thread itself again, so it just takes a blink to update your TextView
public class MyActivity extends Activity {
TextView t;
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... void) {
//do your stuff
return null
}
protected void onPostExecute(Void void) {
MyActivity.this.t.setText("updated ^^v");
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t = (TextView) findViewById(R.id.outputtext);
MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute();
}
}

Categories

Resources