Intent to activity is not working while using fragment for card view on click

android multiple fragments in one activity example
fragment lifecycle
android multiple fragments in same container
recyclerview item click listener in activity android
framelayout fragment android example
replace fragment android
add fragment to activity
change fragment layout programmatically

I am using a fragment to hold 4 card views, which should start a new activity on click. It can display the toast I generate which shows that the click function is working but it is unable to activate the intent.

I have tried using several names for the activity and also

Intent intent = new Intent (getActivity(), activity name.class)

code to start activity

package com.example.eb.ui.home;
import ...
public class HomeFragment extends Fragment implements View.OnClickListener{
    private HomeViewModel homeViewModel;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
  homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final CardView  java = root.findViewById(R.id.javacardId);
        final CardView  html = root.findViewById(R.id.htmlcardId);
        final CardView  c_prog = root.findViewById(R.id.C_cardId);
        final CardView  cpp = root.findViewById(R.id.cppcardId);
           //set on click listener
        java.setOnClickListener(this);
        html.setOnClickListener(this);
        cpp.setOnClickListener(this);
        c_prog.setOnClickListener(this);
             return root;
    }
    @Override
    public void onClick(View v) {
        Intent i;
        switch(v.getId()){
            case R.id.javacardId :
                i = new Intent(this,html.class);
                startActivity(i);
                   break;
            case R.id.htmlcardId :
                i = new Intent(this,html_prog.class);
                startActivity(i);
                 break;
            case R.id.cppcardId :
                i = new Intent(this,cpp_prog.class);
               startActivity(i);
               break;
            case R.id.C_cardId :
                i = new Intent(this,c_prog.class);
                startActivity(i);
                break;
            default: break;
        }

    }
}
---

I expect the card view in that fragment to open up new activity

Try changing the "this" keyword to getContext()

  "i = new Intent(this,html.class);"

should be

  i = new Intent(getContext,html.class);

or initialize and set the clicklistener in onViewCreated() lifecycle method of Fragment.

I want to launch a new activity on clicking a card view in a fragment , By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, onClickListener does not work in fragment (5 answers) Here i am trying to go to a new activity on clicking the cardview. container, false); } public void onClick(View view) { Intent i ; switch (view. From an activity: The context is the current activity (this) Intent intent = new Intent(this, NewActivity.class); startActivity(intent); From a fragment: The context is the parent activity (getActivity()). Notice, that the fragment itself can start the activity via startActivity(), this is not necessary to be done from the activity.

you should use proper names for your activities like HtmlActivity. it should always start with upper case

i = new Intent(getActivity(), HtmlActivity.class);
startActivity(i);

Build a flexible UI, When designing your application to support a wide range of screen sizes at runtime—is that your activity layout must include a container View in In order to replace one fragment with another, the activity's layout the layout file in the previous lesson, but the layout directory does not null, intent.extras) I solved it by creating a listener interface in the fragment, calling that listener's onMyItemClick method in the ViewHolder's onClick and then implementing that listener in my Main Activity. This allowed me to define what to do when an item was clicked while efficiently bubbling up the click events to my main activity.

You have to use getActivity() instead of 'this' in a fragment for context like below.

@Override
public void onClick(View v) {
    Intent i;
    switch(v.getId()){
        case R.id.javacardId :
            i = new Intent(getActivity(),html.class);
            startActivity(i);
               break;
        default: break;
    }

Build a details view, Create a details activity; Define a listener for clicked items class to implement a details view for a media item when it is selected by For more information on using fragment transactions, see the Build a activity, enables you to invoke your details view using an Intent . arrow_back Provide a card view. In this Android tip, I will show you how to send an intent from a fragment to an activity. Sending an intent from a fragment to an activity is almost the same as sending an intent from an activity to another activity except you need to pass a reference of the fragmen's hosting activity to the constructor of the Intent class.

The first parameter required by Intent is Context

As mentioned in the Android documentation (Here):

packageContext  Context: A Context of the application package implementing this class.

Hence, you need to pass getContext() or getActivity() (since Activity extends Context) as follows:

i = new Intent(getActivity(), html.class);
startActivity(i);

I tried building and launching an app using your code (after making the change above) and it worked fine.

Set a click listener to a RecyclerView, Now, in onBindViewHolder the view is assigned with this click listener: Antonio Leiva i want to how to create a on item click listener in fragment..please tell me. A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple

Place Autocomplete | Places SDK for Android, Option 2: Use an intent to launch the autocomplete activity. visual appearance, nest the fragment within another layout element such as a CardView. If you are� Otherwise, the calling activity will start the exit transition, but then you'll see a window transition (like scale or fade). To start an enter transition as soon as possible, use the Window.setAllowEnterTransitionOverlap() function on the called activity. This lets you have more dramatic enter transitions. Start an activity using transitions

Shared Element Activity Transition, Traditionally transitions between different activities or fragments involved < androidx.cardview.widget. Pass data object in the bundle and populate details activity. intent. have multiple views with the same transition name in the source hierarchy, If you are running into issues with shared element transitions such as� In this video I show you how to add an OnClickListener to your recyclerview and display data in a new activity. In the new activity you'll learn to display an image using an ImageView widgets and

RecyclerView OnClickListener to New Activity, WARNING!! I don't use the "best-practice" way to detect clicks to a RecyclerView in this Duration: 15:20 Posted: Jan 2, 2018 Example. Below is a simple application which has one button in one activity and when clicked to goes to other activity using intent and in the other activity, two fragments are shown.

Comments
  • Try initializing and setting the clicklistener in onViewCreated lifecycle method of Fragment.
  • that doesn't change the fact since it is only for testing purpose
  • you should also use getContext() or getActivity() instead of this(because Intent needs context). since you mentioned you already tried that I didn't mention it. use it like this and it works fine
  • The problem probably lies somewhere else (in code not posted) then. This solution should technically work.