0

I've a Java class that represents a Generic User area activity with some buttons, I've to integrate this buttons, texts ecc into a Tabbed Activity. Here's the generic class.

public class UserAreaActivity extends AppCompatActivity {
    private Button scan_btn;
    // User Session Manager Class
    UserSessionManager session;
Context thisContext;

// Button Logout
Button btnLogout;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_area);

    thisContext = getApplicationContext();

    // Session class instance
    session = new UserSessionManager(getApplicationContext());

    final EditText etUsername= (EditText) findViewById(R.id.etUsername);
    final TextView welcomeMessage=(TextView) findViewById(R.id.tvWelcomeMsg);

    // Button logout
    btnLogout = (Button) findViewById(R.id.btnLogout);

//        Toast.makeText(getApplicationContext(),
//                "User Login Status: " + session.isUserLoggedIn(),
//

    //get user data from session
    HashMap<String, String> user = session.getUserDetails();
    //get name
    String name = user.get(UserSessionManager.KEY_NAME);
    // get username
    String username = user.get(UserSessionManager.KEY_USERNAME);



    //Intent intent = getIntent();



    String message = name + " welcome to your user area!";
    welcomeMessage.setText(message);
    etUsername.setText(username);

    scan_btn=(Button) findViewById(R.id.scan_btn);
    final Activity activity = this;

    btnLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Clear the User session data
            // and redirect user to LoginActivity
            session.logoutUser();
        }
    });

    scan_btn.setOnClickListener
            (
                    new View.OnClickListener()
                    {
                        @Override
                        public void onClick(View view)
                        {
                            IntentIntegrator integrator = new IntentIntegrator(activity);
                            integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
                            integrator.setPrompt("Scan");
                            integrator.setCameraId(0);
                            integrator.setBeepEnabled(false);
                            integrator.setBarcodeImageEnabled(false);
                            integrator.initiateScan();
                        }
                    }
            );
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if(result != null)
    {
        if(result.getContents()==null)
        {
            Toast.makeText(this,"Hai cancellato la scansione", Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(this, result.getContents(),Toast.LENGTH_LONG).show();
        }
    }
    else
    {
        super.onActivityResult(requestCode, resultCode, data);
    }

}

}

Since Java doesn't support multiple inheritance how can I extend from AppCompatActivity and Fragment at the same time for integrating into my TabbedView buttons ecc..?

Here's the Fragment where I want to put my code

public class Tab3Add extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab3add, container, false);
    return rootView;
}

}

KolteStar371
  • 73
  • 1
  • 8
  • your question is not clear to me. Actually the fragment are used to do a part of work which is hosted in an activity. Your desired task code will be implemented on Fragment and that fragment will be added on the activity. Check this out http://www.vogella.com/tutorials/AndroidFragments/article.html – Md. Sulayman May 18 '17 at 20:26
  • You can't inherit two class at the same time. It is not possible to extend an Activity and a Fragment at the same time. But you can separate them but add on the activity. The Fragment part will execute according to it's flow on the activity – Md. Sulayman May 18 '17 at 20:27

1 Answers1

0

In this case the best you can do is inflate the Activity's layout in the Fragment and manually move the click handlers and the other code into it while being mindful of the lifecycle differences between an Activity and a Fragment:

public class Tab3Add extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.activity_user_area, container, false);
    // Move your other code as well
    return rootView;
}

Also if there any onClick attributes in R.layout.activity_user_area you need to remove them and add them programatically.

Veneet Reddy
  • 2,707
  • 1
  • 24
  • 40