0

I know there are some topics in StackOverFlow asking this But all of them are answering in special cases No one answers this question generally.
Can anyone say how to use findViewById in non activity classes?
For Example in my case I want to define a webview:

WebView view=(WebView)findViewById(R.id.webview);

But I can't and I really don't understand why android makes everything complicated.Why you can use everything in main activity but you can't use many of them in non activity class :(
UPDATE
I can't extend my class as activity cause it extends sth else.
UPDATE
This is the class:

        class DownloadTask extends AsyncTask<String, Void, Void>  {
    protected Void doInBackground(String... sUrl) {
        ...
                        WebView view=(WebView)findViewById(R.id.webview);
                        final Snackbar snackbar = Snackbar.make(view,"message",Snackbar.LENGTH_LONG);
        ...
    }
}

I'm gonna use the webview in a Snackbar.

SAM
  • 281
  • 3
  • 15

4 Answers4

3

Can anyone say how to use findViewById in non activity classes?

Pass the activity into the method you are calling on the non-activity class.

Or, pass the activity into the constructor of the non-activity class.

Or, call findViewById() in the activity and pass the WebView to the non-activity class.

Plenty of other patterns exist. You need to be careful that you do not try using the activity or WebView longer than you should (e.g., after the user rotates the screen, presses BACK, or otherwise causes the activity to be destroyed).

I'm gonna use the webview in a Snackbar.

Using a Snackbar is fine. Using one from an AsyncTask is not, unless you do so very carefully. In particular:

  • Only try doing this from the main application thread (i.e., not doInBackground())

  • Make sure that you are handling configuration changes, BACK presses, and the like properly. In particular, you cannot show a Snackbar on a destroyed activity

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @SAM: If I am going to use `AsyncTask`, I often use a retained fragment, such as in [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/Threads/AsyncTask). That sample does not show the use of `Snackbar`, but it updates the fragment and shows a `Toast`. The use of `AsyncTask`, and updating your UI based on its results, is covered in any modern book or course on Android app development. – CommonsWare Feb 05 '17 at 18:52
1

1-in non activity class:

public Context con;
public NonAct(Activity c){//NonAct = your non activity class name
    this.con = c;
}

//in your class or function do this:
WebView view=(WebView)con.findViewById(R.id.webview);

2-call non activity class with sending context argumant:

NonAct n = new NonAct(YourActivity.this);
0

try this

ClassName instance = new ClassName(this);

and starting of your class will look like this

public class ClassName extents to something {

public Activity activity; 
//.... other attributes 

public ClassName( Activity _activity){

   this.activity = _activity;
//other initializations...

you can use findviewby id like this

WebView mywebview = (WebView)this.activity.findViewById(R.id.mywebview);

///// now do whatever u want

i hope this will help u.

Abdul.Moqueet
  • 902
  • 12
  • 19
0

just look at this link u have to pass your view inflated to AsyncTask

findviewbyid in asyncTask

Community
  • 1
  • 1
Abdul.Moqueet
  • 902
  • 12
  • 19