Created
July 28, 2013 18:01
-
-
Save jromero/6099470 to your computer and use it in GitHub Desktop.
Use a fragment from a separate package (app) while maintaining resources intact. (Not tested extensively, specially with themes). Note: The remote fragment should be self sufficient and wouldn't be able to interact with dependancies of the main application or vise-versa. [See http://stackoverflow.com/questions/10792136/classcastexception-while-d…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.api.extension; | |
import android.app.Fragment; | |
import android.content.Context; | |
import android.content.pm.PackageManager.NameNotFoundException; | |
import android.os.Bundle; | |
import android.text.TextUtils; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
public class RemoteFragment extends Fragment { | |
public static final String ARG_PACKAGE_NAME = "ARG_PACKAGE_NAME"; | |
private Context mRemoteContext; | |
public Context getRemoteContext() { | |
return mRemoteContext; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
Bundle args = getArguments(); | |
String packageName = args.getString(ARG_PACKAGE_NAME); | |
if (TextUtils.isEmpty(packageName)) { | |
throw new RuntimeException("Package name cannot be null!"); | |
} | |
try { | |
// Get the remote package context | |
mRemoteContext = getActivity().createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY); | |
} catch (NameNotFoundException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
@Override | |
public final View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Create the LayoutInflater from remote context so resources | |
// are retrieved appropriately | |
return onCreateRemoteView(LayoutInflater.from(mRemoteContext), container, savedInstanceState); | |
} | |
/** | |
* Override to create view from remote context. | |
* See {@link Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle)} | |
* | |
* @param inflater | |
* @param container | |
* @param savedInstanceState | |
* @return | |
*/ | |
public View onCreateRemoteView(LayoutInflater inflater, | |
ViewGroup container, Bundle savedInstanceState) { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment