마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

프래그먼트에서 프래그먼트로 화면전환 질문드립니다.

0 추천
package com.example.myapplication.ui.foodrecommend;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapplication.MainActivity;
import com.example.myapplication.R;
import com.example.myapplication.ui.rdfood.RandomFragment;

import java.util.Random;

public class RandomActivity extends Fragment {

    Button button;
    @Nullable
    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.foodrecommemd, container, false);

        button = v.findViewById(R.id.randomButton);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), RandomFragment.class);
                startActivity(intent);
            }
        });

        return v;
    }


}

해당 프래그먼트 소스인데 버튼을 눌러서 RandomFragment라는 클래스의 프래그먼트로 이동하고 싶습니다.

임시적으로 intent를 쓰긴 하였으나 실행이 안되었고 다른방법으로 찾고자 하는데 잘모르겠어서 질문을 올립니다

연쿡 (120 포인트) 님이 2020년 9월 28일 질문
연쿡님이 2020년 9월 28일 reshown

1개의 답변

0 추천

RandomFragment.class는 Fragment입니다.

startActivity() 하려면, 다음의 두번째 인자에는 Activity가 들어가야합니다.

Intent intent = new Intent(getActivity(), RandomFragment.class);

 

디자이너정 (42,810 포인트) 님이 2020년 9월 28일 답변
RandomFragment클래스로 이동하기 위해서는 어떤걸 써야 됩니까??
...