PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label android-fragments. Show all posts
Showing posts with label android-fragments. Show all posts

Tuesday, July 12, 2022

[FIXED] How Send message from fragment to activity and received and use in activity?

 July 12, 2022     android, android-activity, android-fragments, message     No comments   

Issue

Please please don't minus my question i confused when googling. I used Android Tab Layout with Swipeable Views in my code for when user pressed setting button on an activity.

now I need send message from TopRatedFragment.java that extends from fragment to the activity that call the mainActivity of "Android Tab Layout with Swipeable Views".


Solution

Here's the solution:

Step 1 : From your fragment.

Intent i = new Intent(getActivity(), YourActivity.class);
        i.putExtra("key", "Your value1");
        i.putExtra("key2", "Your value2");
        i.putExtra("key3", "Your value3");
        getActivity().startActivity(i);

Step 2 : In your Activity where you want the result

Intent getResults = getIntent();
        String firstValue = getResults.getStringExtra("key1");
        String secondValue = getResults.getStringExtra("key2");
        String thirdValue = getResults.getStringExtra("key3");

Use those values your needs are.

Hope this helps.. :)



Answered By - mike20132013
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, April 21, 2022

[FIXED] How to run Volley in fragment(Navigation Drawer)?

 April 21, 2022     android, android-fragments, android-volley, connection, navigation-drawer     No comments   

Issue

I want to call volley by clicking new item in navigation drawer.
How to run Volley from Fragment? I can run volley easily in activities but it is showing error in "this". i.e.

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);

in above code How to run volley in Fragment? in Navigation Drawer?

I am doing this:

 public CollegesFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_colleges, container, false);


        // Instantiate the RequestQueue.
   RequestQueue queue = Volley.newRequestQueue(this);



        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // Display the first 500 characters of the response string.
                Toast.makeText(getActivity(), response, Toast.LENGTH_LONG).show();
                Log.d("TAG", "hello");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        return rootView;
    }

please give me idea.


Solution

before returning the rootView you need to add the following line:

queue.add(stringRequest);

You need to add the request to the RequestQueue.

Also, Volley.newRequestQueue(context) takes a Context as parameter. So you should pass the activity context or the application context.

RequestQueue queue = Volley.newRequestQueue(getActivity().getApplicationContext());

Hope this helps



Answered By - denvercoder9
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing