Soru & Cevap

Fragment'ın işleyişi ...

26.07.2019 - 02:15

Merhaba, fragment yaşam döngüsünü anlamakta problem çekiyorum. 4 fragmenttan oluşan bir tabbar uygulaması yapıyorum, fragment'ın içinde kodları nasıl yazacağımı anlayamadım, yaptığım işlemler diğer fragment'ı açıp tekrar işlem olmasını beklediğim fragment'a gelince çalışıyor, yaşam döngüsünde karşılığı olan fonksiyonu bulamadım. Özetlersek fragmentta tarih girip butona bastığımda bana ekranda tarih farkını vermesini bekiyorum ama diğer fragmentı açıp tekrar ilgiliye gelince çalışıyor. Kodlar;


public class fragmentTimer extends Fragment {
    @Nullable
    TextView textView;
    EditText dateText;
    Button buttonTime;
    long b;
    SharedPreferences preferences;
    int beforeAfter;
    boolean isPast;


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //buttonTime = (Button) view.findViewById(R.id.button);
    }

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmenttimer, container, false);


        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final TextView textView = (TextView) getView().findViewById(R.id.textView4);
        dateText = getView().findViewById(R.id.dateText);

        buttonTime = (Button) getView().findViewById(R.id.button);
        buttonTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Date userDate = new Date();
                Date currDate = new Date();
                System.out.printf("%1$s %2$tB %2$td, %2$tY", "Due date:", currDate);
                String str = String.format("Current Date/Time : %tc", currDate);
                //textView.setText(str);
                String dateString;
                String fromDate = "01/07/2019";
                dateString = dateText.getText().toString();
                String fromDate2 = dateString;
                DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                Date dtt = null;
                try {
                    dtt = df.parse(fromDate2);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                java.sql.Date ds = new java.sql.Date(dtt.getTime());
                beforeAfter = currDate.compareTo(dtt);
                if(beforeAfter > 0){
                    b = currDate.getTime() - dtt.getTime();
                    isPast = true;

                }else if(beforeAfter < 0){
                    b = dtt.getTime() - currDate.getTime();
                    isPast = false;
                }
            }
        });

        int saniye,dakika,saat,gun;
        b= b/1000;
        saniye = (int) (b % 60);
        b = b / 60;
        dakika = (int) (b % 60);
        b = b / 60;
        saat = (int) (b % 24);
        b= b / 24;
        gun = (int) b;
        String durum = "";
        if(isPast == true){
            durum = "Geçti";
        }
        else if(isPast == false){
            durum = "Kaldı";
        }
        //textView.setText("saniye :" + saniye + " dakika :" + dakika + "\n saat :" + saat + "gün :" + gun);

            textView.setText(+gun + " Gün " + saat + " Saat " + dakika + " Dakika " + saniye + " Saniye " + durum + " ");
    }

    public void buttonTime9(View view) {
        Date userDate = new Date();
        Date currDate = new Date();
        System.out.printf("%1$s %2$tB %2$td, %2$tY", "Due date:", currDate);
        String str = String.format("Current Date/Time : %tc", currDate);
        textView.setText(str);
        String dateString;
        String fromDate = "19/05/2009";
        dateString = dateText.getText().toString();
        String fromDate2 = dateString;
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        Date dtt = null;
        try {
            dtt = df.parse(fromDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date ds = new java.sql.Date(dtt.getTime());
        long b = currDate.getTime() - dtt.getTime();
        textView.setText("Fark" + b);
        System.out.println("fark " +  b);
    }

}

 

21 Görüntülenme

2 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user ardakazanc
ardakazanc
27.07.2019 - 04:14

onCreateView içerisinde View elemanlarını referanslara bağla. (findViewById ...)

Daha sonra işlem fonksiyonlarına göre sonucu ilgili referanslarda göster. 

Burada ki kritik nokta. Fragment'tan Activity içerisinde mi göstermek. 

İyi çalışmalar. 

 

picture-19298-1459404577.jpg
alper_beyler
27.07.2019 - 12:04

Merhaba, fragment için lifecyle incelemelisin. https://medium.com/@bugraburunguz/activity-and-fragment-lifecycle-39a3c77a2df6. ayrıca sen işlemleri onActivityCreated da yapıyorsun o kodları onCreateView bloğu içinde yapmalısın.