B
B
bychok3002016-12-16 13:08:47
Java
bychok300, 2016-12-16 13:08:47

How to switch to a new activity and process the process at the same time?

There is such an example

public class MainActivity extends AppCompatActivity {

    private Button button;
    private TextView textView;

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

        button = (Button)findViewById(R.id.button);
        textView = (TextView)findViewById(R.id.textView1);
    }

    public void ClickMe(View v) {
        MyTask mt = new MyTask();
        mt.execute();

        //Intent intent = new Intent(MainActivity.this, Parsed.class);
        //startActivity(intent);

    }

    class MyTask extends AsyncTask<Void, Void, Void> {

        String html;//Тут храним значение заголовка сайта

        @Override
        protected Void doInBackground(Void... params) {

            Document doc = null;//Здесь хранится будет разобранный html документ
            try {
                //Считываем заглавную страницу http://harrix.org
                doc = Jsoup.connect("http://habrahabr.ru").get();
            } catch (IOException e) {
                //Если не получилось считать
                e.printStackTrace();
            }

            //Если всё считалось, то вытаскиваем из считанного html документа всё
            if (doc!=null)
                html = doc.html();
            else
                html = "Ошибка";

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            textView.setText(html);
        }
    }
}

how to make it so that when you click on the button, there would be a transition to a new activity and everything that was parsed was displayed there?
I shoved pieces of code here and there, but I didn’t achieve the result, tell me what and where to transfer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2016-12-16
@bychok300

1 AsyncTask is not intended for http requests
2 Move the logic to the second Activity,
but it is better to use the Service

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question