2012-11-19 8 views
10

संभव डुप्लिकेट:
How do I pass data between activities in Android?आप अन्य गतिविधियों के साथ चर को आसानी से कैसे साझा करते हैं?

मैं एक कार्ड खेल बना रहा हूँ और मैं त्यागकर कार्ड के लिए एक गतिविधि और स्कोर को दिखाने के लिए एक गतिविधि है। समस्या यह है कि मैं कुछ ऑब्जेक्ट्स (प्लेयर और डीलर हैंड) को दूसरी गतिविधि में पास करना चाहता हूं ताकि मैं खिलाड़ियों के हाथों में कार्ड में छवि दृश्यों को सेट कर सकूं। मैं यह कैसे कर सकता हूँ? मुझे सुरक्षा या कुछ भी पसंद नहीं है जो मैं सबसे आसान तरीका चाहता हूं।

+2

सबसे आसान तरीका उपयोग बंडलों अवधारणा –

+0

कैसे आप बंडलों का उपयोग करते हैं जिन वस्तुओं पर एक स्ट्रिंग या कुछ नहीं कर रहे हैं भेजने के लिए है? –

+0

नहीं क्योंकि वे आपको सिखाते हैं कि तारों जैसी बुनियादी चीजें कैसे करें ... –

उत्तर

6

हमें इरादे के अंदर आईएनजी बंडल सुरक्षा के बारे में नहीं है, ऐसा इसलिए है क्योंकि एंड्रॉइड लड़कों ने इसे सादा और सरल बना दिया है। बड़ी वस्तुओं को पारित करने के लिए बंडलों और इरादों का उपयोग करके मेरी राय में एक अच्छा विचार नहीं है। यह कार्यान्वित करने के लिए बहुत जटिल हो जाता है, जिससे आप ऑब्जेक्ट को प्राइमेटिव्स (पार्ससेलबल का उपयोग करते समय) पर ले जाते हैं और स्मृति में दूसरी तरफ एक प्रतिलिपि बनाते हैं (आप एक ऑब्जेक्ट लेते हैं, इरादे के अंदर सबकुछ सेट करते हैं और फिर इसे फिर से बनाते हैं दूसरी तरफ से एक नई प्रतिलिपि बना रही है) जो ऑब्जेक्ट्स के लिए एक बड़ी मेमोरी पदचिह्न अच्छी नहीं है।

मेरा सुझाव है:

  1. या तो एक सिंगलटन दुकान
  2. का उपयोग कर आवेदन वर्ग (जो भी एक सिंगलटन की तरह कार्य करता)

का उपयोग करते हुए मैं अक्सर एक सिंगलटन जो एक hashmap है उपयोग कर रहा हूँ अंदर जहां एक पूर्णांक कुंजी मेरे द्वारा उत्पन्न होती है (परमाणु इंटीजर से) और मानचित्र के अंदर रखी गई वस्तु। आप केवल इरादे के अंदर आईडी को अतिरिक्त के रूप में भेजते हैं और उद्देश्य से कुंजी प्राप्त करके और ऑब्जेक्ट को पुनर्प्राप्त करने और उस वस्तु को हटाने (उस मानचित्र से) को हटाने और इसे अपनी नई गतिविधि/सेवा में उपयोग करने के लिए दूसरी तरफ इसे पुनर्प्राप्त करते हैं।

यहाँ कुछ इस तरह का एक नमूना है:

(नोट: इस मामले में बाकी अनुरोधों के लिए मेरी lib (https://github.com/darko1002001/android-rest-client) से एक हिस्सा है आप सब कुछ लागू करने के तरीके के बारे में अधिक जानकारी देखना चाहते हैं)। आपके मामले में आपको कुछ कोड पट्टी करने और इसे अपने आप से बदलने की आवश्यकता होगी, लेकिन सामान्य विचार समान है।

/** 
* @author Darko.Grozdanovski 
*/ 
public class HttpRequestStore { 

    public static final String TAG = HttpRequestStore.class.getSimpleName(); 

    public static final String KEY_ID = "id"; 
    public static final String IS_SUCCESSFUL = "isSuccessful"; 

    private static final HashMap<Integer, RequestWrapper> map = new HashMap<Integer, RequestWrapper>(); 

    private final AtomicInteger counter = new AtomicInteger(); 
    private static Class<?> executorServiceClass = HTTPRequestExecutorService.class; 

    private final Context context; 
    private static HttpRequestStore instance; 

    private HttpRequestStore(final Context context) { 
     this.context = context; 
    } 

    public static HttpRequestStore getInstance(final Context context) { 
     if (instance == null) { 
      instance = new HttpRequestStore(context.getApplicationContext()); 
     } 
     return instance; 
    } 

    public static void init(final Class<?> executorServiceClass) { 
     HttpRequestStore.executorServiceClass = executorServiceClass; 
    } 

    public Integer addRequest(final RequestWrapper block) { 
     return addRequest(counter.incrementAndGet(), block); 
    } 

    public Integer addRequest(final Integer id, final RequestWrapper block) { 
     map.put(id, block); 
     return id; 
    } 

    public void removeBlock(final Integer id) { 
     map.remove(id); 
    } 

    public RequestWrapper getRequest(final Integer id) { 
     return map.remove(id); 
    } 

    public RequestWrapper getRequest(final Intent intent) { 
     final Bundle extras = intent.getExtras(); 
     if (extras == null || extras.containsKey(KEY_ID) == false) { 
      throw new RuntimeException("Intent Must be Filled with ID of the block"); 
     } 
     final int id = extras.getInt(KEY_ID); 
     return getRequest(id); 
    } 

    public Integer launchServiceIntent(final HttpRequest block) { 
     return launchServiceIntent(block, null); 
    } 

    public Integer launchServiceIntent(final HttpRequest block, RequestOptions options) { 
     if (executorServiceClass == null) { 
      throw new RuntimeException("Initialize the Executor service class in a class extending application"); 
     } 
     if (isServiceAvailable() == false) { 
      throw new RuntimeException("Declare the " + executorServiceClass.getSimpleName() + " in your manifest"); 
     } 
     final Intent service = new Intent(context, executorServiceClass); 
     final RequestWrapper wrapper = new RequestWrapper(block, options); 
     final Integer requestId = addRequest(wrapper); 
     service.putExtra(KEY_ID, requestId); 
     context.startService(service); 
     return requestId; 
    } 

    public boolean isServiceAvailable() { 
     final PackageManager packageManager = context.getPackageManager(); 
     final Intent intent = new Intent(context, executorServiceClass); 
     final List<ResolveInfo> resolveInfo = packageManager.queryIntentServices(intent, 
       PackageManager.MATCH_DEFAULT_ONLY); 
     if (resolveInfo.size() > 0) { 
      return true; 
     } 
     return false; 
    } 

} 
0

आप शेयर वैरिएबल के लिए बंडल या साझा वरीयताओं का उपयोग कर सकते हैं या भविष्य के उपयोग के लिए चर बचा सकते हैं। साझा वरीयताओं के लिए

उदाहरण आप बंडलों के लिए here उदाहरण पा सकते हैं आप पा सकते हैं here

0

सिंगलटन सबसे अच्छा तरीका हो सकता है

0

आप आशय एक्स्ट्रा कलाकार का उपयोग कर सकते,

Intent intent = new Intent(getBaseContext(), NewActivity.class); 
intent.putExtra("DATA_KEY", data); 
startActivity(intent) 

docs के लिए इरादों में अधिक जानकारी है ("एक्स्ट्रा" शीर्षक वाले अनुभाग को देखें)।

1

आप अन्य गतिविधियों में चर साझा करने के लिए बंडल का उपयोग कर सकते हैं। आप में अन्य गतिविधियों के अपने वर्ग

यहाँ के Parcelable का उपयोग अपने स्वयं के वर्ग वस्तु पास करना चाहते हैं एक उदाहरण है

public class Person implements Parcelable { 
    private int age; 
    private String name;  

    // Setters and Getters 
    // .... 


    public int describeContents() { 
     return 0; 
    } 

    public void writeToParcel(Parcel out, int flags) { 
     out.writeString(name); 
     out.writeInt(age); 
    } 

    public static final Parcelable.Creator<Person> CREATOR 
      = new Parcelable.Creator<Person>() { 
     public Person createFromParcel(Parcel in) { 
      return new Person(in); 
     } 

     public Person[] newArray(int size) { 
      return new Person[size]; 
     } 
    }; 

    private Person(Parcel in) { 
     name = in.readString(); 
     age = in.readInt(); 
    } 
} 

बंडल

Intent i = new Intent(); 
Bundle b = new Bundle(); 
b.putParcelable("bob", new Person()); 

में अपने Person वस्तु सम्मिलित हो रही Person वस्तु

Intent i = getIntent(); 
Bundle b = i.getExtras(); 

Person p = (Person) b.getParcelable("bob"); 
+0

आपकी मदद के लिए धन्यवाद, दोनों उत्तरों ने मेरी मदद की लेकिन मैं दोनों को स्वीकार नहीं कर सकता। –

संबंधित मुद्दे