2

I would like to display activity_main on fragment button click.

Is there something wrong with my code?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.splash, container, false);

   Button smart1 = (Button) myView.findViewById(R.id.smart1);
    smart1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
           getActivity().onBackPressed();

        }

    });


    return myView;
}

Button is not working. It just stays on the same page and nothing happens.

Edited for Nas

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;


public class Splash extends Fragment {

    View myView;


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

        return myView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Button smart1 = (Button) view.findViewById(R.id.smart1);
        smart1.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
                getActivity().onBackPressed();

        }

        });


    }
}

Here is the Splash.java, it's weird why buttons aren't working. Thank you in advance.

Aaron
  • 23
  • 5
  • and what you want to do? – ρяσѕρєя K Jan 14 '17 at 04:01
  • @ρяσѕρєяK I would like to get the data of my MainActivity onBackPressed – Aaron Jan 14 '17 at 04:04
  • I think this is what you want: http://stackoverflow.com/a/41310395/6632568 – beeb Jan 14 '17 at 04:12
  • What are you trying to actually do? You aren't getting anything. You're calling the method that is called in response to back press, not actually simulating a back action. Are you wanting to finish the activity? – OneCricketeer Jan 14 '17 at 04:25
  • @cricket_007 I would like to transfer to layout activity_main from my fragment button click. – Aaron Jan 14 '17 at 04:31
  • Then call `getActivity().setContentView`... Or load a Fragment using that layout. The Fragment or FrameLayout is already a part of activity_main, no? – OneCricketeer Jan 14 '17 at 04:33
  • @cricket_007 No sir, it's not part of activity main. Should I replace getActivity().backPressed to getActivity().setContentView? – Aaron Jan 14 '17 at 04:35
  • Not necessarily, that's just one option. Can you show `splash.xml`,please – OneCricketeer Jan 14 '17 at 04:36
  • @cricket_007 [link](http://pastebin.com/HC2X2met) here you go sir – Aaron Jan 14 '17 at 04:40
  • can you add onBackPressed of activity and splash.xml code..this code looks fine. – santosh kumar Jan 14 '17 at 04:41
  • Assuming u have navigated from main to splash.... call getActivity().finish() ,this would stop ur current activity and take u back to last activity. Or second approach is to Define what you wish to do onBackPressed in the activity first – Ak9637 Jan 14 '17 at 04:41
  • @santoshkumar here is my onBackPressed code: [link](http://pastebin.com/sQMfGnWZ) – Aaron Jan 14 '17 at 04:44
  • Don't use PasteBin, please [edit] your question – OneCricketeer Jan 14 '17 at 04:49
  • You're doing way too much work in `onBackPressed`. I am very confused what you want or why you are trying to load a completely separate activity view within an existing activity – OneCricketeer Jan 14 '17 at 04:51
  • Use `this.getActivity().getSupportFragmentManager().popBackStack();` for go back in fragments – Ahmad Vatani Jan 14 '17 at 05:14
  • onBackPressed is override method .. do you want to create method with the same name as of onBackPressed() ? – santosh kumar Jan 14 '17 at 05:17
  • Yes @santoshkumar but I have some concerns. Is there anyway to test the button click? I think it's not working at all. I even tried adding alert box and still not working – Aaron Jan 14 '17 at 05:25
  • have you debug it? does it show any log on clicking? – Nas Jan 14 '17 at 05:44
  • @Nas no logs shown after click. :/ – Aaron Jan 14 '17 at 05:47
  • @Aaron set onclick listener in onViewCreated method or onActivityCreated method if still not working keep clickable true for the parent layout in your case RelativeLayout – Nas Jan 14 '17 at 05:52
  • @Nas Where to find onViewCreated and onActivityCreated? I'm sorry I'm new here. – Aaron Jan 14 '17 at 05:55

1 Answers1

0

try like this

@Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    Button smart1 = (Button) view.findViewById(R.id.smart1);
    smart1.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
       getActivity().onBackPressed();

    }

});
Nas
  • 2,158
  • 1
  • 21
  • 38
  • Thank you but it still not doing anything on button click. – Aaron Jan 14 '17 at 06:07
  • then try to keep clickable true for the parent layout in your case RelativeLayout – Nas Jan 14 '17 at 06:09
  • keep clickable true for your button smart1 too. still not working?? – Nas Jan 14 '17 at 06:14
  • Yes sir. android:clickable="true" still not giving me any. – Aaron Jan 14 '17 at 06:17
  • no am told that keep clickable for both parent layout as well as the button view you are trying to fire an event – Nas Jan 14 '17 at 06:20
  • But splash.xml doesn't have a parent layout sir. – Aaron Jan 14 '17 at 06:23
  • your RelativeLayout is parent layout for the button smart1 so try to add clickable true for both smart1 and RelativeLayout . – Nas Jan 14 '17 at 06:35
  • I tried adding it to my RelativeLayout and smart1 still not working – Aaron Jan 14 '17 at 06:42
  • have you implement onclick listener in your fragment calss if so pls post your full fragment class code. – Nas Jan 14 '17 at 06:47
  • seems to be no pblm with your code remove getActivity().onBackPressed(); and show a toast inside and check whether toast showing or not. – Nas Jan 14 '17 at 07:14
  • Tried Toast.makeText(getActivity(),"Button is working",Toast.LENGTH_SHORT).show(); and nothing showing up. What seems to be the problem? :/ – Aaron Jan 14 '17 at 07:19
  • I tried it using another fragment and toast is working fine on button click. What could be the problem? – Aaron Jan 14 '17 at 07:28
  • no idea. if possible share your project through GitHub. let me check. – Nas Jan 14 '17 at 07:30