Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7978387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:28:55+00:00 2026-06-04T09:28:55+00:00

In order to simplify some code, I’m wondering if it is possible to parameterize

  • 0

In order to simplify some code, I’m wondering if it is possible to parameterize code that launches activities in an android application, so that instead of having 5

    public void showSettings(View view) {
    Intent SettingsActivity = new Intent(MainActivity.this, Settings.class);
    startActivity(SettingsActivity);

I can do something like the following

 public void showActivity(View view, String ActivityName) {
        Intent ActivityName = new Intent(MainActivity.this, ActivityName.class);
        startActivity(ActivityName);

Then, for each button in the UI, I simply apply the following to the “onclick” event

showActivity(Settings);

or

showActivity(domains);

This would save about 40-50 lines of code in my app. Obviously I know the above code is incorrect, but I’m not sure if it’s possible to do what I’m trying to accomplish.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:28:57+00:00Added an answer on June 4, 2026 at 9:28 am

    How about something like:

    public <T> void showActivity(View view, Class<T> activity) {
        Intent activityName = new Intent(MainActivity.this, activity);
        startActivity(activityName);
    }
    

    You can invoke it with

    showActivity(Settings.class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am parsing some enum values from a text file. In order to simplify
Now I got a TabHost with several tabs, and in order to simplify the
Trying to implement some nested loops that are spitting out good old nested html
I'm trying to do something that is fairly simple, but my code looks terrible
I currently have a listing of data that has a randomly generated order listing.
In order to simplify the build process, I've been trying to reorganize my version
I'm trying to simplify some mysql. I have three selects and three tables: SELECT
Here's the scenario: I want define a base class that allocate some buffer once
Out of order execution in CPUs means that a CPU can reorder instructions to
in a C++ program I need some helper constant objects that would be instantiated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.