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 8547621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:18:18+00:00 2026-06-11T13:18:18+00:00

I have a problem by reading getSharedPreferences from non-Activity class to set playlist in

  • 0

I have a problem by reading getSharedPreferences from non-Activity class to set playlist in player… In my Activity I take string variable from edittext to get path of folder to work with audio files…

    public class MainActivity extends Activity {


String ppp;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    String PATH = getSharedPreferences("PATH", MODE_PRIVATE).getString("path", ppp);
    if (PATH == null){
        ..........
            ...........
        path_tv.setText("folder is undefined");
    }
    else {
        path_tv.setText("folder defined: /mnt/sdcard/" + PATH);
    }
    set_path.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (path_edit.getText().toString().length() == 0){
                Toast.makeText(getBaseContext(), "folder is undefined", Toast.LENGTH_SHORT).show();
            }
            else {
            ppp = path_edit.getText().toString();
            getSharedPreferences("PATH", MODE_PRIVATE)
            .edit()
            .putString("path", ppp)
            .commit();
            File folder = new File(Environment.getExternalStorageDirectory() + "/" + ppp);
            boolean success = false;
            if (!folder.exists()) {
                success = folder.mkdir();
                if (success) Toast.makeText(getBaseContext(), ".....", Toast.LENGTH_SHORT).show();
            }
            String PATH = getSharedPreferences("PATH", MODE_PRIVATE).getString("path", ppp);
            path_tv.setText("........ /mnt/sdcard/" + PATH);
            path_edit.setText("");
            }
        }
    });

So, in activity I can change and save value of String in shared preferences… But ho can I do it from public class…? Appreciate any examples…

  • 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-11T13:18:19+00:00Added an answer on June 11, 2026 at 1:18 pm

    One way is to use Application object. This is a dirty hack but none the less sometimes helpful.

    First you need a static member in your Application class, so:

    public class MyApplication extends Application {
        private static MyApplication instance;
    
        @Override
        public void onCreate() {
            super.onCreate();
            instance = this;
        }
    
        public static MyApplication getInstance() {
            return instance;
        }
    }
    

    Since Application object is always created before any activity is created and run and is kept throughout application lifetime you can always be sure you will have proper one.

    Then in your code simply call MyApplication.getInstance() and you will have global app context.

    Remember to declare MyApplication in manifest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

WCF service on IIS. I have got a problem with reading connection string from
Android 2.1 update 1 Eclipse 3.5 I have a problem reading data from my
I have some problem with reading from sqlite3 database. -(void) readMessengesFromDatabase { sqlite3 *database;
My problem involves checking if I have a valid database connection before reading from
I have a problem reading OWL/XML files from Java using Jena. I have no
I have a problem with reading empty string in C. I want to read
I have a problem reading and using the content from unicode files. I am
i have problem getting text from edittext inside listview when im in last edittext
I have a problem with reading the image path of a string like ->
I have problem when creating a file in encoding 'utf-8' and reading it from

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.